private String toSimpleName(String name) { String simpleName = name.substring(name.lastIndexOf(".")+1); if (Strings.isBlank(simpleName)) simpleName = name.trim(); return Strings.makeValidFilename(simpleName); }
/** this assumes the download is an archive containing a single directory on the root which will be renamed to "cookbookName"; * if that directory already has the correct name cookbookName can be null, * but if e.g. taking from a github tarball it will typically be of the form cookbookName-master/ * hence the renaming */ // TODO support installing from classpath, and using the repository (tie in with those methods) public static final String downloadAndExpandCookbook(String source, @Nullable String cookbookName, boolean force) { String dl = downloadAndExpandCookbook(source); if (cookbookName==null) return dl; String tmpName = "tmp-"+Strings.makeValidFilename(cookbookName)+"-"+Identifiers.makeRandomId(4); String installCmd = BashCommands.chain("mkdir "+tmpName, "cd "+tmpName, dl, BashCommands.requireTest("`ls | wc -w` -eq 1", "The downloaded archive must contain exactly one directory; contained"), "COOKBOOK_EXPANDED_DIR=`ls`", "mv $COOKBOOK_EXPANDED_DIR '../"+cookbookName+"'", "cd ..", "rm -rf "+tmpName); if (!force) return BashCommands.alternatives("ls "+cookbookName, installCmd); else return BashCommands.alternatives("rm -rf "+cookbookName, installCmd); }