Search in sources :

Example 1 with JitPackUtil

use of dev.jbang.dependencies.JitPackUtil in project jbang by jbangdev.

the class Edit method createProjectForEdit.

/**
 * Create Project to use for editing *
 */
File createProjectForEdit(SourceSet ss, RunContext ctx, boolean reload) throws IOException {
    File originalFile = ss.getResourceRef().getFile();
    List<String> dependencies = ss.getDependencies();
    String cp = ss.getClassPath().getClassPath();
    List<String> resolvedDependencies = Arrays.asList(cp.split(CP_SEPARATOR));
    File baseDir = Settings.getCacheDir(Cache.CacheClass.projects).toFile();
    String name = originalFile.getName();
    name = Util.unkebabify(name);
    File tmpProjectDir = new File(baseDir, name + "_jbang_" + Util.getStableID(originalFile.getAbsolutePath()));
    tmpProjectDir.mkdirs();
    tmpProjectDir = new File(tmpProjectDir, stripPrefix(name));
    tmpProjectDir.mkdirs();
    File srcDir = new File(tmpProjectDir, "src");
    srcDir.mkdir();
    Path srcFile = srcDir.toPath().resolve(name);
    Util.createLink(srcFile, originalFile.toPath());
    for (Source source : ss.getSources()) {
        File sfile = null;
        if (source.getJavaPackage().isPresent()) {
            File packageDir = new File(srcDir, source.getJavaPackage().get().replace(".", File.separator));
            packageDir.mkdirs();
            sfile = new File(packageDir, source.getResourceRef().getFile().getName());
        } else {
            sfile = new File(srcDir, source.getResourceRef().getFile().getName());
        }
        Path destFile = source.getResourceRef().getFile().toPath().toAbsolutePath();
        Util.createLink(sfile.toPath(), destFile);
    }
    for (RefTarget ref : ss.getResources()) {
        File target = ref.to(srcDir.toPath()).toFile();
        target.getParentFile().mkdirs();
        Util.createLink(target.toPath(), ref.getSource().getFile().toPath().toAbsolutePath());
    }
    // create build gradle
    Optional<String> packageName = Util.getSourcePackage(new String(Files.readAllBytes(srcFile), Charset.defaultCharset()));
    String baseName = Util.getBaseName(name);
    String fullClassName;
    fullClassName = packageName.map(s -> s + "." + baseName).orElse(baseName);
    String templateName = "build.qute.gradle";
    Path destination = new File(tmpProjectDir, "build.gradle").toPath();
    TemplateEngine engine = TemplateEngine.instance();
    // both collectDependencies and repositories are manipulated by
    // resolveDependencies
    List<MavenRepo> repositories = ss.getRepositories();
    if (repositories.isEmpty()) {
        ss.addRepository(DependencyUtil.toMavenRepo("mavencentral"));
    }
    // Turn any URL dependencies into regular GAV coordinates
    List<String> depIds = dependencies.stream().map(JitPackUtil::ensureGAV).collect(Collectors.toList());
    // And if we encountered URLs let's make sure the JitPack repo is available
    if (!depIds.equals(dependencies) && repositories.stream().noneMatch(r -> DependencyUtil.REPO_JITPACK.equals(r.getUrl()))) {
        ss.addRepository(DependencyUtil.toMavenRepo(DependencyUtil.ALIAS_JITPACK));
    }
    renderTemplate(engine, depIds, fullClassName, baseName, resolvedDependencies, repositories, templateName, ctx.getArguments(), destination);
    // setup eclipse
    templateName = ".qute.classpath";
    destination = new File(tmpProjectDir, ".classpath").toPath();
    renderTemplate(engine, dependencies, fullClassName, baseName, resolvedDependencies, repositories, templateName, ctx.getArguments(), destination);
    templateName = ".qute.project";
    destination = new File(tmpProjectDir, ".project").toPath();
    renderTemplate(engine, dependencies, fullClassName, baseName, resolvedDependencies, repositories, templateName, ctx.getArguments(), destination);
    templateName = "main.qute.launch";
    destination = new File(tmpProjectDir, ".eclipse/" + baseName + ".launch").toPath();
    destination.toFile().getParentFile().mkdirs();
    renderTemplate(engine, dependencies, fullClassName, baseName, resolvedDependencies, repositories, templateName, ctx.getArguments(), destination);
    templateName = "main-port-4004.qute.launch";
    destination = new File(tmpProjectDir, ".eclipse/" + baseName + "-port-4004.launch").toPath();
    renderTemplate(engine, dependencies, fullClassName, baseName, resolvedDependencies, repositories, templateName, ctx.getArguments(), destination);
    // setup vscode
    templateName = "launch.qute.json";
    destination = new File(tmpProjectDir, ".vscode/launch.json").toPath();
    if (isNeeded(reload, destination)) {
        destination.toFile().getParentFile().mkdirs();
        renderTemplate(engine, dependencies, fullClassName, baseName, resolvedDependencies, repositories, templateName, ctx.getArguments(), destination);
    }
    // setup vscode
    templateName = "README.qute.md";
    destination = new File(tmpProjectDir, "README.md").toPath();
    if (isNeeded(reload, destination)) {
        destination.toFile().getParentFile().mkdirs();
        renderTemplate(engine, dependencies, fullClassName, baseName, resolvedDependencies, repositories, templateName, ctx.getArguments(), destination);
    }
    templateName = "settings.qute.json";
    destination = new File(tmpProjectDir, ".vscode/settings.json").toPath();
    if (isNeeded(reload, destination)) {
        destination.toFile().getParentFile().mkdirs();
        renderTemplate(engine, dependencies, fullClassName, baseName, resolvedDependencies, repositories, templateName, ctx.getArguments(), destination);
    }
    return tmpProjectDir;
}
Also used : Path(java.nio.file.Path) java.util(java.util) MavenRepo(dev.jbang.dependencies.MavenRepo) System.out(java.lang.System.out) WatchKey(java.nio.file.WatchKey) dev.jbang.source(dev.jbang.source) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) JitPackUtil(dev.jbang.dependencies.JitPackUtil) CP_SEPARATOR(dev.jbang.Settings.CP_SEPARATOR) Charset(java.nio.charset.Charset) EditorManager(dev.jbang.net.EditorManager) TemplateEngine(dev.jbang.util.TemplateEngine) Path(java.nio.file.Path) CommandLine(picocli.CommandLine) Util(dev.jbang.util.Util) Files(java.nio.file.Files) WatchEvent(java.nio.file.WatchEvent) DependencyUtil(dev.jbang.dependencies.DependencyUtil) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Shell(dev.jbang.util.Util.Shell) File(java.io.File) WatchService(java.nio.file.WatchService) Template(io.quarkus.qute.Template) BaseBuilder.escapeOSArguments(dev.jbang.source.builders.BaseBuilder.escapeOSArguments) Settings(dev.jbang.Settings) Util.verboseMsg(dev.jbang.util.Util.verboseMsg) FileSystems(java.nio.file.FileSystems) Cache(dev.jbang.Cache) TemplateEngine(dev.jbang.util.TemplateEngine) MavenRepo(dev.jbang.dependencies.MavenRepo) File(java.io.File)

Aggregations

Cache (dev.jbang.Cache)1 Settings (dev.jbang.Settings)1 CP_SEPARATOR (dev.jbang.Settings.CP_SEPARATOR)1 DependencyUtil (dev.jbang.dependencies.DependencyUtil)1 JitPackUtil (dev.jbang.dependencies.JitPackUtil)1 MavenRepo (dev.jbang.dependencies.MavenRepo)1 EditorManager (dev.jbang.net.EditorManager)1 dev.jbang.source (dev.jbang.source)1 BaseBuilder.escapeOSArguments (dev.jbang.source.builders.BaseBuilder.escapeOSArguments)1 TemplateEngine (dev.jbang.util.TemplateEngine)1 Util (dev.jbang.util.Util)1 Shell (dev.jbang.util.Util.Shell)1 Util.verboseMsg (dev.jbang.util.Util.verboseMsg)1 Template (io.quarkus.qute.Template)1 File (java.io.File)1 IOException (java.io.IOException)1 System.out (java.lang.System.out)1 Charset (java.nio.charset.Charset)1 FileSystems (java.nio.file.FileSystems)1 Files (java.nio.file.Files)1