/** * Expands the variables in the given string by using environment variables represented in 'this'. */ public String expand(String s) { return Util.replaceMacro(s, this); }
/** * Resolves environment variables against each other. */ public static void resolve(Map<String, String> env) { for (Map.Entry<String,String> entry: env.entrySet()) { entry.setValue(Util.replaceMacro(entry.getValue(), env)); } }
/** * Replaces the occurrence of '$key' by {@code properties.get('key')}. * * <p> * Unlike shell, undefined variables are left as-is (this behavior is the same as Ant.) * */ @Nullable public static String replaceMacro( @CheckForNull String s, @Nonnull Map<String,String> properties) { return replaceMacro(s,new VariableResolver.ByMap<String>(properties)); }
/** * Performs a variable substitution to the given text and return it. */ public String substitute(AbstractBuild<?,?> build, String text) { return Util.replaceMacro(text,createVariableResolver(build)); }
private File getExeFile(String execName) { String m2Home = Util.replaceMacro(getHome(),EnvVars.masterEnvVars); if(Functions.isWindows()) { File exeFile = new File(m2Home, "bin/" + execName + ".bat"); // since Maven 3.3 .bat files are replaced with .cmd if (!exeFile.exists()) { return new File(m2Home, "bin/" + execName + ".cmd"); } return exeFile; } else { return new File(m2Home, "bin/" + execName); } }
/** * Recursively expands the variables in text and attribute values and return the new DOM. * * The expansion uses {@link Util#replaceMacro(String, VariableResolver)}, so any unresolved * references will be left as-is. */ public XStreamDOM expandMacro(VariableResolver<String> vars) { String[] newAttributes = new String[attributes.length]; for (int i=0; i<attributes.length; i+=2) { newAttributes[i+0] = attributes[i]; // name newAttributes[i+1] = Util.replaceMacro(attributes[i+1],vars); } List<XStreamDOM> newChildren = null; if (children!=null) { newChildren = new ArrayList<XStreamDOM>(children.size()); for (XStreamDOM d : children) newChildren.add(d.expandMacro(vars)); } return new XStreamDOM(tagName,newAttributes,newChildren,Util.replaceMacro(value,vars)); }
@Restricted(NoExternalUse.class) public static String expandVariablesForDirectory(String base, String itemFullName, String itemRootDir) { return Util.replaceMacro(base, ImmutableMap.of( "JENKINS_HOME", Jenkins.getInstance().getRootDir().getPath(), "ITEM_ROOTDIR", itemRootDir, "ITEM_FULLNAME", itemFullName, // legacy, deprecated "ITEM_FULL_NAME", itemFullName.replace(':','$'))); // safe, see JENKINS-12251 }
private static String replaceMacros(Run<?, ?> build, TaskListener listener, String inputString) { String returnString = inputString; if (build != null && inputString != null) { try { Map<String, String> messageEnvVars = getEnvVars(build, listener); returnString = Util.replaceMacro(inputString, messageEnvVars); } catch (Exception e) { listener.getLogger().printf("Couldn't replace macros in message: %s%n", e.getMessage()); LOGGER.log(Level.WARNING, "Couldn't replace macros in message", e); } } return returnString; }
/** * Adds key value pairs as "-Dkey=value -Dkey=value ..." by parsing a given string using {@link Properties} with masking. * * @param prefix * The '-D' portion of the example. Defaults to -D if null. * @param properties * The persisted form of {@link Properties}. For example, "abc=def\nghi=jkl". Can be null, in which * case this method becomes no-op. * @param vr * {@link VariableResolver} to resolve variables in properties string. * @param propsToMask * Set containing key names to mark as masked in the argument list. Key * names that do not exist in the set will be added unmasked. * @since 1.378 */ public ArgumentListBuilder addKeyValuePairsFromPropertyString(String prefix, String properties, VariableResolver<String> vr, Set<String> propsToMask) throws IOException { if(properties==null) return this; properties = Util.replaceMacro(properties, propertiesGeneratingResolver(vr)); for (Entry<Object,Object> entry : Util.loadProperties(properties).entrySet()) { addKeyValuePair(prefix, (String)entry.getKey(), entry.getValue().toString(), (propsToMask == null) ? false : propsToMask.contains(entry.getKey())); } return this; }
Util.replaceMacro(entry.getValue(), resolver);
@Override public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener) { if (StringUtils.isEmpty(path)) { return null; } try { EnvVars env = build.getEnvironment(listener); String targetPath = Util.replaceMacro(this.path, build.getBuildVariableResolver()); targetPath = env.expand(targetPath); if (IOUtils.isAbsolute(targetPath)) { return new FilePath(new File(targetPath)); } else { FilePath mrSettings = build.getModuleRoot().child(targetPath); FilePath wsSettings = build.getWorkspace().child(targetPath); try { if (!wsSettings.exists() && mrSettings.exists()) { wsSettings = mrSettings; } } catch (Exception e) { throw new IllegalStateException("failed to find settings.xml at: " + wsSettings.getRemote()); } return wsSettings; } } catch (Exception e) { throw new IllegalStateException("failed to prepare settings.xml"); } }
@Override public FilePath supplySettings(AbstractBuild<?, ?> build, TaskListener listener) { if (StringUtils.isEmpty(path)) { return null; } try { EnvVars env = build.getEnvironment(listener); String targetPath = Util.replaceMacro(this.path, build.getBuildVariableResolver()); targetPath = env.expand(targetPath); if (IOUtils.isAbsolute(targetPath)) { return new FilePath(new File(targetPath)); } else { FilePath mrSettings = build.getModuleRoot().child(targetPath); FilePath wsSettings = build.getWorkspace().child(targetPath); try { if (!wsSettings.exists() && mrSettings.exists()) { wsSettings = mrSettings; } } catch (Exception e) { throw new IllegalStateException("failed to find settings.xml at: " + wsSettings.getRemote()); } return wsSettings; } } catch (Exception e) { throw new IllegalStateException("failed to prepare global settings.xml"); } }
try (InputStream in = url.openStream()) { String literal = IOUtils.toString(in,"UTF-8"); rsp.getWriter().println(Util.replaceMacro(literal, Collections.singletonMap("rootURL",req.getContextPath())));
String targets = Util.replaceMacro(this.targets,vr); targets = env.expand(targets); String pom = env.expand(this.pom);
/** * Expands the variables in the given string by using environment variables represented in 'this'. */ public String expand(String s) { return Util.replaceMacro(s, this); }
/** * Resolves environment variables against each other. */ public static void resolve(Map<String, String> env) { for (Map.Entry<String,String> entry: env.entrySet()) { entry.setValue(Util.replaceMacro(entry.getValue(), env)); } }
public static String replaceMacros(Run<?, ?> build, TaskListener listener, String inputString) { String returnString = inputString; if (build != null && inputString != null) { try { Map<String, String> messageEnvVars = getEnvVars(build, listener); returnString = Util.replaceMacro(inputString, messageEnvVars); } catch (Exception e) { LOGGER.log(Level.SEVERE, "Couldn't replace macros in message: ", e); } } return returnString; }
/** * Replaces the occurrence of '$key' by <tt>properties.get('key')</tt>. * * <p> * Unlike shell, undefined variables are left as-is (this behavior is the same as Ant.) * */ @Nullable public static String replaceMacro( @CheckForNull String s, @Nonnull Map<String,String> properties) { return replaceMacro(s,new VariableResolver.ByMap<String>(properties)); }
/** * Performs a variable subsitution to the given text and return it. */ public String substitute(AbstractBuild<?,?> build, String text) { return Util.replaceMacro(text,createVariableResolver(build)); }
@Restricted(NoExternalUse.class) public static String expandVariablesForDirectory(String base, String itemFullName, String itemRootDir) { return Util.replaceMacro(base, ImmutableMap.of( "JENKINS_HOME", Jenkins.getInstance().getRootDir().getPath(), "ITEM_ROOTDIR", itemRootDir, "ITEM_FULLNAME", itemFullName, // legacy, deprecated "ITEM_FULL_NAME", itemFullName.replace(':','$'))); // safe, see JENKINS-12251 }