| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.scriptbuilder.domain; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import com.google.common.base.CaseFormat; |
| 26 | import com.google.common.base.Function; |
| 27 | import com.google.common.collect.MapMaker; |
| 28 | import com.google.common.collect.Maps; |
| 29 | |
| 30 | /** |
| 31 | * Constants used in shell scripting. |
| 32 | * |
| 33 | * @author Adrian Cole |
| 34 | */ |
| 35 | public enum ShellToken { |
| 36 | |
| 37 | FS, RM, CD, TMP, UID, ROOT, CLOSE_FD, PS, MD, ESCVAR, |
| 38 | |
| 39 | /** |
| 40 | * If variable values need to be quoted when they include spaces, this will contain quotation |
| 41 | * mark |
| 42 | */ |
| 43 | VQ, |
| 44 | /** |
| 45 | * Left hand side of the function declaration directly before the name of the function. |
| 46 | */ |
| 47 | FNCL, |
| 48 | /** |
| 49 | * Right hand side of the function declaration directly after the name of the function. opens the |
| 50 | * code block |
| 51 | */ |
| 52 | FNCR, |
| 53 | /** |
| 54 | * End the function. exits successfully and closes the code block. |
| 55 | */ |
| 56 | FNCE, BEGIN_SCRIPT, END_SCRIPT, BEGIN_FUNCTIONS, EXIT, END_FUNCTIONS, EXPORT, LF, SH, SOURCE, REM, RETURN, ARGS, VARL, VARR, LIBRARY_PATH_VARIABLE; |
| 57 | |
| 58 | private static final Map<OsFamily, Map<String, String>> familyToTokenValueMap = new MapMaker() |
| 59 | .makeComputingMap(new Function<OsFamily, Map<String, String>>() { |
| 60 | |
| 61 | @Override |
| 62 | public Map<String, String> apply(OsFamily from) { |
| 63 | Map<String, String> map = Maps.newHashMap(); |
| 64 | for (ShellToken token : ShellToken.values()) { |
| 65 | map.put(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, token |
| 66 | .toString()), token.to(from)); |
| 67 | } |
| 68 | return map; |
| 69 | } |
| 70 | |
| 71 | }); |
| 72 | |
| 73 | public static Map<String, String> tokenValueMap(OsFamily family) { |
| 74 | return familyToTokenValueMap.get(family); |
| 75 | } |
| 76 | |
| 77 | public String to(OsFamily family) { |
| 78 | checkNotNull(family, "family"); |
| 79 | switch (this) { |
| 80 | case FS: |
| 81 | switch (family) { |
| 82 | case WINDOWS: |
| 83 | return "\\"; |
| 84 | case UNIX: |
| 85 | return "/"; |
| 86 | } |
| 87 | case FNCL: |
| 88 | switch (family) { |
| 89 | case WINDOWS: |
| 90 | return ":"; |
| 91 | case UNIX: |
| 92 | return "function "; |
| 93 | } |
| 94 | case FNCR: |
| 95 | switch (family) { |
| 96 | case WINDOWS: |
| 97 | return "\r\n"; |
| 98 | case UNIX: |
| 99 | return " {\n"; |
| 100 | } |
| 101 | case FNCE: |
| 102 | switch (family) { |
| 103 | case WINDOWS: |
| 104 | return " exit /b 0\r\n"; |
| 105 | case UNIX: |
| 106 | return " return 0\n}\n"; |
| 107 | } |
| 108 | case ESCVAR: |
| 109 | switch (family) { |
| 110 | case WINDOWS: |
| 111 | return "%"; |
| 112 | case UNIX: |
| 113 | return "\\"; |
| 114 | } |
| 115 | case PS: |
| 116 | switch (family) { |
| 117 | case WINDOWS: |
| 118 | return ";"; |
| 119 | case UNIX: |
| 120 | return ":"; |
| 121 | } |
| 122 | case CLOSE_FD: |
| 123 | switch (family) { |
| 124 | case WINDOWS: |
| 125 | return ">NUL"; |
| 126 | case UNIX: |
| 127 | return ">&-"; |
| 128 | } |
| 129 | case RM: |
| 130 | switch (family) { |
| 131 | case WINDOWS: |
| 132 | return "del"; |
| 133 | case UNIX: |
| 134 | return "rm"; |
| 135 | } |
| 136 | case MD: |
| 137 | switch (family) { |
| 138 | case WINDOWS: |
| 139 | return "md"; |
| 140 | case UNIX: |
| 141 | return "mkdir -p"; |
| 142 | } |
| 143 | case VQ: |
| 144 | switch (family) { |
| 145 | case WINDOWS: |
| 146 | return ""; |
| 147 | case UNIX: |
| 148 | return "\""; |
| 149 | } |
| 150 | case BEGIN_FUNCTIONS: |
| 151 | switch (family) { |
| 152 | case WINDOWS: |
| 153 | return "GOTO FUNCTION_END\r\n"; |
| 154 | case UNIX: |
| 155 | return ""; |
| 156 | } |
| 157 | case END_FUNCTIONS: |
| 158 | switch (family) { |
| 159 | case WINDOWS: |
| 160 | return ":FUNCTION_END\r\n"; |
| 161 | case UNIX: |
| 162 | return ""; |
| 163 | } |
| 164 | case BEGIN_SCRIPT: |
| 165 | switch (family) { |
| 166 | case WINDOWS: |
| 167 | return "@echo off\r\n"; |
| 168 | case UNIX: |
| 169 | return "#!/bin/bash\nset +u\nshopt -s xpg_echo\nshopt -s expand_aliases\n"; |
| 170 | } |
| 171 | case END_SCRIPT: |
| 172 | switch (family) { |
| 173 | case WINDOWS: |
| 174 | return "exit /b 0\r\n"; |
| 175 | case UNIX: |
| 176 | return "exit 0\n"; |
| 177 | } |
| 178 | case EXPORT: |
| 179 | switch (family) { |
| 180 | case WINDOWS: |
| 181 | return "set"; |
| 182 | case UNIX: |
| 183 | return "export"; |
| 184 | } |
| 185 | case RETURN: |
| 186 | switch (family) { |
| 187 | case WINDOWS: |
| 188 | return "exit /b"; |
| 189 | case UNIX: |
| 190 | return "return"; |
| 191 | } |
| 192 | case EXIT: |
| 193 | switch (family) { |
| 194 | case WINDOWS: |
| 195 | return "exit /b"; |
| 196 | case UNIX: |
| 197 | return "exit"; |
| 198 | } |
| 199 | case ROOT: |
| 200 | switch (family) { |
| 201 | case WINDOWS: |
| 202 | return "c:\\"; |
| 203 | case UNIX: |
| 204 | return "/"; |
| 205 | } |
| 206 | case TMP: |
| 207 | switch (family) { |
| 208 | case WINDOWS: |
| 209 | return "%TEMP%"; |
| 210 | case UNIX: |
| 211 | return "/tmp"; |
| 212 | } |
| 213 | case UID: |
| 214 | switch (family) { |
| 215 | case WINDOWS: |
| 216 | return "%USERNAME%"; |
| 217 | case UNIX: |
| 218 | return "$USER"; |
| 219 | } |
| 220 | case LF: |
| 221 | switch (family) { |
| 222 | case WINDOWS: |
| 223 | return "\r\n"; |
| 224 | case UNIX: |
| 225 | return "\n"; |
| 226 | } |
| 227 | case SH: |
| 228 | switch (family) { |
| 229 | case WINDOWS: |
| 230 | return "cmd"; |
| 231 | case UNIX: |
| 232 | return "sh"; |
| 233 | } |
| 234 | case LIBRARY_PATH_VARIABLE: |
| 235 | switch (family) { |
| 236 | case WINDOWS: |
| 237 | return "PATH"; |
| 238 | case UNIX: |
| 239 | return "LD_LIBRARY_PATH"; |
| 240 | } |
| 241 | case SOURCE: |
| 242 | switch (family) { |
| 243 | case WINDOWS: |
| 244 | return "@call"; |
| 245 | case UNIX: |
| 246 | return "."; |
| 247 | } |
| 248 | case CD: |
| 249 | switch (family) { |
| 250 | case WINDOWS: |
| 251 | return "cd /d"; |
| 252 | case UNIX: |
| 253 | return "cd"; |
| 254 | } |
| 255 | case REM: |
| 256 | switch (family) { |
| 257 | case WINDOWS: |
| 258 | return "@rem"; |
| 259 | case UNIX: |
| 260 | return "#"; |
| 261 | } |
| 262 | case ARGS: |
| 263 | switch (family) { |
| 264 | case WINDOWS: |
| 265 | return "%*"; |
| 266 | case UNIX: |
| 267 | return "$@"; |
| 268 | } |
| 269 | case VARL: |
| 270 | switch (family) { |
| 271 | case WINDOWS: |
| 272 | return "%"; |
| 273 | case UNIX: |
| 274 | return "$"; |
| 275 | } |
| 276 | case VARR: |
| 277 | switch (family) { |
| 278 | case WINDOWS: |
| 279 | return "%"; |
| 280 | case UNIX: |
| 281 | return ""; |
| 282 | } |
| 283 | default: |
| 284 | throw new UnsupportedOperationException("token " + this + " not configured"); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | } |