private void copy(File workspaceDir, InputStream in, Pattern glob, boolean overwrite) throws Exception { Jar jar = new Jar("dot", in); try { for (Entry<String, Resource> e : jar.getResources().entrySet()) { String path = e.getKey(); bnd.trace("path %s", path); if (glob != null && !glob.matcher(path).matches()) continue; Resource r = e.getValue(); File dest = Processor.getFile(workspaceDir, path); if (overwrite || !dest.isFile() || dest.lastModified() < r.lastModified() || r.lastModified() <= 0) { bnd.trace("copy %s to %s", path, dest); File dp = dest.getParentFile(); if (!dp.exists() && !dp.mkdirs()) { throw new IOException("Could not create directory " + dp); } IO.copy(r.openInputStream(), dest); } } } finally { jar.close(); } }
String ls(String args[], boolean relative) { if (args.length < 2) throw new IllegalArgumentException( "the ${ls} macro must at least have a directory as parameter"); File dir = domain.getFile(args[1]); if (!dir.isAbsolute()) throw new IllegalArgumentException( "the ${ls} macro directory parameter is not absolute: " + dir); if (!dir.exists()) throw new IllegalArgumentException( "the ${ls} macro directory parameter does not exist: " + dir); if (!dir.isDirectory()) throw new IllegalArgumentException( "the ${ls} macro directory parameter points to a file instead of a directory: " + dir); Collection<File> files = new ArrayList<File>(new SortedList<File>(dir.listFiles())); for (int i = 2; i < args.length; i++) { Instructions filters = new Instructions(args[i]); files = filters.select(files, true); } List<String> result = new ArrayList<String>(); for (File file : files) result.add(relative ? file.getName() : file.getAbsolutePath()); return Processor.join(result, ","); }
/** * Get the contents of a file. * * @param in * @return * @throws IOException */ public String _cat(String args[]) throws IOException { verifyCommand(args, "${cat;<in>}, get the content of a file", null, 2, 2); File f = domain.getFile(args[1]); if (f.isFile()) { return IO.collect(f); } else if (f.isDirectory()) { return Arrays.toString(f.list()); } else { try { URL url = new URL(args[1]); return IO.collect(url, "UTF-8"); } catch (MalformedURLException mfue) { // Ignore here } return null; } }
public String _basename(String args[]) { if (args.length < 2) { domain.warning("Need at least one file name for ${basename;...}"); return null; } String del = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i < args.length; i++) { File f = domain.getFile(args[i]); if (f.exists() && f.getParentFile().exists()) { sb.append(del); sb.append(f.getName()); del = ","; } } return sb.toString(); }
public void _workspace(WorkspaceOptions opts) throws Exception { File base = bnd.getBase(); String name = opts._arguments().get(0); File workspaceDir = Processor.getFile(base, name); name = workspaceDir.getName(); base = workspaceDir.getParentFile(); if (base == null) { bnd.error( "You cannot create a workspace in the root (%s). The parent of a workspace %n" + "must be a valid directory. Recommended is to dedicate a directory to %n" + "all (or related) workspaces.", workspaceDir); return; } if (!opts.anyname() && !Verifier.isBsn(name)) { bnd.error( "You specified a workspace name that does not follow the recommended pattern " + "(it should be like a Bundle Symbolic name). It is a bit pedantic but it " + "really helps hwne you get many workspaces. If you insist on this name, use the -a/--anyname option."); return; } Workspace ws = bnd.getWorkspace((File) null); if (ws != null && ws.isValid()) { bnd.error( "You are currently in a workspace already (%s) in %s. You can only create a new workspace outside an existing workspace", ws, base); return; } File eclipseDir = workspaceDir; workspaceDir.mkdirs(); if (!opts.single()) workspaceDir = new File(workspaceDir, "scm"); workspaceDir.mkdirs(); if (!base.isDirectory()) { bnd.error("Could not create directory for the bnd workspace %s", base); } else if (!eclipseDir.isDirectory()) { bnd.error("Could not create directory for the Eclipse workspace %s", eclipseDir); } if (!workspaceDir.isDirectory()) { bnd.error("Could not create the workspace directory %s", workspaceDir); return; } if (!opts.update() && !opts.force() && workspaceDir.list().length > 0) { bnd.error( "The workspace directory %s is not empty, specify -u/--update to update or -f/--force to replace", workspaceDir); } InputStream in = getClass().getResourceAsStream("/templates/enroute.zip"); if (in == null) { bnd.error("Cannot find template in this jar %s", "/templates/enroute.zip"); return; } Pattern glob = Pattern.compile("[^/]+|cnf/.*|\\...+/.*"); copy(workspaceDir, in, glob, opts.force()); File readme = new File(workspaceDir, "README.md"); if (readme.isFile()) IO.copy(readme, bnd.out); bnd.out.printf( "%nWorkspace %s created.%n%n" // + " Start Eclipse:%n" // + " 1) Select the Eclipse workspace %s%n" // + " 2) Package Explorer context menu: Import/General/Existing Projects from %s%n" + "%n" + "", // workspaceDir.getName(), eclipseDir, workspaceDir); }
public String _osfile(String args[]) { verifyCommand(args, _fileHelp, null, 3, 3); File base = new File(args[1]); File f = Processor.getFile(base, args[2]); return f.getAbsolutePath(); }