| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.scriptbuilder; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import org.jclouds.scriptbuilder.domain.OsFamily; |
| 26 | import org.jclouds.scriptbuilder.domain.ShellToken; |
| 27 | import org.jclouds.scriptbuilder.util.Utils; |
| 28 | |
| 29 | import com.google.common.annotations.VisibleForTesting; |
| 30 | import com.google.common.collect.ImmutableMap; |
| 31 | import com.google.common.collect.Maps; |
| 32 | |
| 33 | /** |
| 34 | * Creates an environment file |
| 35 | * |
| 36 | * @author Adrian Cole |
| 37 | */ |
| 38 | public class EnvBuilder { |
| 39 | |
| 40 | @VisibleForTesting |
| 41 | Map<String, String> variables = Maps.newHashMap(); |
| 42 | |
| 43 | /** |
| 44 | * Exports a variable inside the script |
| 45 | */ |
| 46 | public EnvBuilder export(String name, String value) { |
| 47 | variables.put(checkNotNull(name, "name"), checkNotNull(value, "value")); |
| 48 | return this; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * builds the environment file, by adding the following |
| 53 | * <ol> |
| 54 | * <li>example usage</li> |
| 55 | * <li>variable exports</li> |
| 56 | * <li>return statement</li> |
| 57 | * </ol> |
| 58 | * |
| 59 | * @param osFamily |
| 60 | * whether to write a cmd or bash script. |
| 61 | */ |
| 62 | public String build(final OsFamily osFamily) { |
| 63 | StringBuilder builder = new StringBuilder(); |
| 64 | builder.append(Utils.writeComment(" Env file: please do not confuse people by making this executable", osFamily)); |
| 65 | builder.append(Utils.writeComment("", osFamily)); |
| 66 | builder.append(Utils.writeComment(" Example usage to set a variable", osFamily)); |
| 67 | builder.append(Utils.writeComment("", osFamily)); |
| 68 | builder.append(Utils.writeComment(" " |
| 69 | + Utils.writeVariableExporters(ImmutableMap.of("mavenOpts", "-Xms64m -Xmx128m"), |
| 70 | osFamily), osFamily)); |
| 71 | builder.append(Utils.writeVariableExporters(variables, osFamily)); |
| 72 | builder.append(ShellToken.LF.to(osFamily)); |
| 73 | builder.append(Utils.writeComment( |
| 74 | " Please retain this statement so that the script can be validated", osFamily)); |
| 75 | builder.append(ShellToken.RETURN.to(osFamily)).append(" 0") |
| 76 | .append(ShellToken.LF.to(osFamily)); |
| 77 | return builder.toString(); |
| 78 | } |
| 79 | } |