Search in sources :

Example 96 with ST

use of edu.princeton.cs.algs4.ST in project che by eclipse.

the class GwtXmlGenerator method generateGwtXml.

public File generateGwtXml() throws IOException {
    File gwtXml = new File(config.getGenerationRoot(), config.getGwtFileName());
    if (gwtXml.isDirectory() || gwtXml.exists()) {
        throw new IOException(gwtXml.getAbsolutePath() + " already exists or directory");
    }
    ST template = getTemplate();
    template.add("config", config);
    // flush content
    FileUtils.writeStringToFile(gwtXml, template.render());
    return gwtXml;
}
Also used : ST(org.stringtemplate.v4.ST) IOException(java.io.IOException) File(java.io.File)

Example 97 with ST

use of edu.princeton.cs.algs4.ST in project buck by facebook.

the class ProjectGenerator method getBuildWithBuckShellScript.

private String getBuildWithBuckShellScript(TargetNode<?, ?> targetNode) {
    ST template;
    try {
        template = new ST(Resources.toString(Resources.getResource(ProjectGenerator.class, BUILD_WITH_BUCK_TEMPLATE), Charsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException("There was an error loading '" + BUILD_WITH_BUCK_TEMPLATE + "' template", e);
    }
    String buildFlags = getBuildFlags();
    String escapedBuildTarget = Escaper.escapeAsBashString(targetNode.getBuildTarget().getFullyQualifiedName());
    Optional<String> productName = getProductNameForTargetNode(targetNode);
    String binaryName = AppleBundle.getBinaryName(targetNode.getBuildTarget(), productName);
    Path bundleDestination = getScratchPathForAppBundle(projectFilesystem, targetNode.getBuildTarget(), binaryName);
    Path dsymDestination = getScratchPathForDsymBundle(projectFilesystem, targetNode.getBuildTarget(), binaryName);
    Path resolvedBundleDestination = projectFilesystem.resolve(bundleDestination);
    Path resolvedDsymDestination = projectFilesystem.resolve(dsymDestination);
    ImmutableSet<Flavor> flavors = ImmutableSet.copyOf(targetNode.getBuildTarget().getFlavors());
    CxxPlatform cxxPlatform = cxxPlatforms.getValue(flavors).orElse(defaultCxxPlatform);
    String oldCompDir = cxxPlatform.getCompilerDebugPathSanitizer().getCompilationDirectory();
    // Use the hostname for padding instead of the directory, this way the directory matches without
    // having to resolve it.
    String dsymPaddedCompDirWithHost = Strings.padStart(":" + projectFilesystem.getRootPath().toString(), oldCompDir.length(), 'f');
    template.add("path_to_build_with_buck_py", getBuildWithBuckPythonScriptPath(projectFilesystem));
    template.add("path_to_fix_uuid_script", getFixUUIDScriptPath(projectFilesystem));
    template.add("repo_root", projectFilesystem.getRootPath());
    template.add("path_to_buck", getPathToBuck(executableFinder, environment));
    template.add("build_flags", buildFlags);
    template.add("escaped_build_target", escapedBuildTarget);
    template.add("buck_dwarf_flavor", (appleConfig.forceDsymModeInBuildWithBuck() ? AppleDebugFormat.DWARF_AND_DSYM : AppleDebugFormat.DWARF).getFlavor().getName());
    template.add("buck_dsym_flavor", AppleDebugFormat.DWARF_AND_DSYM.getFlavor().getName());
    template.add("binary_name", binaryName);
    template.add("comp_dir", oldCompDir);
    template.add("new_comp_dir", projectFilesystem.getRootPath().toString());
    template.add("padded_source_dir", dsymPaddedCompDirWithHost);
    template.add("resolved_bundle_destination", resolvedBundleDestination);
    template.add("resolved_bundle_destination_parent", resolvedBundleDestination.getParent());
    template.add("resolved_dsym_destination", resolvedDsymDestination);
    template.add("force_dsym", appleConfig.forceDsymModeInBuildWithBuck() ? "true" : "false");
    return template.render();
}
Also used : SourceTreePath(com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) ST(org.stringtemplate.v4.ST) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) IOException(java.io.IOException) NSString(com.dd.plist.NSString) Flavor(com.facebook.buck.model.Flavor)

Example 98 with ST

use of edu.princeton.cs.algs4.ST in project buck by facebook.

the class BuckPyFunction method toPythonFunction.

public String toPythonFunction(BuildRuleType type, Object dto) {
    @Nullable TargetName defaultName = dto.getClass().getAnnotation(TargetName.class);
    ImmutableList.Builder<StParamInfo> mandatory = ImmutableList.builder();
    ImmutableList.Builder<StParamInfo> optional = ImmutableList.builder();
    for (ParamInfo param : ImmutableSortedSet.copyOf(argMarshaller.getAllParamInfo(dto))) {
        if (isSkippable(param)) {
            continue;
        }
        if (param.isOptional()) {
            optional.add(new StParamInfo(param));
        } else {
            mandatory.add(new StParamInfo(param));
        }
    }
    optional.add(StParamInfo.ofOptionalValue("autodeps", "autodeps"));
    optional.add(StParamInfo.ofOptionalValue("visibility", "visibility"));
    STGroup group = buckPyFunctionTemplate.get();
    ST st;
    // See discussion in: https://github.com/antlr/stringtemplate4/issues/61
    synchronized (group) {
        st = group.getInstanceOf("buck_py_function");
    }
    st.add("name", type.getName());
    // Mandatory params must come before optional ones.
    st.add("params", ImmutableList.builder().addAll(mandatory.build()).addAll(optional.build()).build());
    st.add("typePropName", TYPE_PROPERTY_NAME);
    st.add("defaultName", defaultName == null ? null : defaultName.name());
    StringWriter stringWriter = new StringWriter();
    try {
        st.write(new AutoIndentWriter(stringWriter, "\n"));
    } catch (IOException e) {
        throw new IllegalStateException("ST writer should not throw with StringWriter", e);
    }
    return stringWriter.toString();
}
Also used : AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) ST(org.stringtemplate.v4.ST) STGroup(org.stringtemplate.v4.STGroup) StringWriter(java.io.StringWriter) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Example 99 with ST

use of edu.princeton.cs.algs4.ST in project buck by facebook.

the class PythonInPlaceBinary method getScript.

private static Supplier<String> getScript(final BuildRuleResolver resolver, final PythonPlatform pythonPlatform, final CxxPlatform cxxPlatform, final String mainModule, final PythonPackageComponents components, final Path relativeLinkTreeRoot, final ImmutableSet<String> preloadLibraries) {
    final String relativeLinkTreeRootStr = Escaper.escapeAsPythonString(relativeLinkTreeRoot.toString());
    final Linker ld = cxxPlatform.getLd().resolve(resolver);
    return () -> {
        ST st = new ST(getRunInplaceResource()).add("PYTHON", pythonPlatform.getEnvironment().getPythonPath()).add("MAIN_MODULE", Escaper.escapeAsPythonString(mainModule)).add("MODULES_DIR", relativeLinkTreeRootStr);
        // Only add platform-specific values when the binary includes native libraries.
        if (components.getNativeLibraries().isEmpty()) {
            st.add("NATIVE_LIBS_ENV_VAR", "None");
            st.add("NATIVE_LIBS_DIR", "None");
        } else {
            st.add("NATIVE_LIBS_ENV_VAR", Escaper.escapeAsPythonString(ld.searchPathEnvVar()));
            st.add("NATIVE_LIBS_DIR", relativeLinkTreeRootStr);
        }
        if (preloadLibraries.isEmpty()) {
            st.add("NATIVE_LIBS_PRELOAD_ENV_VAR", "None");
            st.add("NATIVE_LIBS_PRELOAD", "None");
        } else {
            st.add("NATIVE_LIBS_PRELOAD_ENV_VAR", Escaper.escapeAsPythonString(ld.preloadEnvVar()));
            st.add("NATIVE_LIBS_PRELOAD", Escaper.escapeAsPythonString(Joiner.on(':').join(preloadLibraries)));
        }
        return st.render();
    };
}
Also used : ST(org.stringtemplate.v4.ST) Linker(com.facebook.buck.cxx.Linker)

Example 100 with ST

use of edu.princeton.cs.algs4.ST in project buck by facebook.

the class LogConfig method addInputStreamForTemplate.

private static boolean addInputStreamForTemplate(Path path, LogConfigSetup logConfigSetup, ImmutableList.Builder<InputStream> inputStreamsBuilder) throws IOException {
    try {
        String template = new String(Files.readAllBytes(path), Charsets.UTF_8);
        ST st = new ST(template);
        st.add("default_file_pattern", MorePaths.pathWithUnixSeparators(logConfigSetup.getLogFilePath()).toString());
        st.add("default_count", logConfigSetup.getCount());
        st.add("default_max_size_bytes", logConfigSetup.getMaxLogSizeBytes());
        String result = st.render();
        inputStreamsBuilder.add(new ByteArrayInputStream(result.getBytes(Charsets.UTF_8)));
        inputStreamsBuilder.add(new ByteArrayInputStream(NEWLINE));
        return true;
    } catch (FileNotFoundException e) {
        return false;
    }
}
Also used : ST(org.stringtemplate.v4.ST) ByteArrayInputStream(java.io.ByteArrayInputStream) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

ST (org.stringtemplate.v4.ST)197 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)37 STGroup (org.stringtemplate.v4.STGroup)24 File (java.io.File)19 ArrayList (java.util.ArrayList)16 IOException (java.io.IOException)12 STGroupFile (org.stringtemplate.v4.STGroupFile)12 Path (java.nio.file.Path)10 Test (org.junit.Test)10 ATNFactory (org.antlr.v4.automata.ATNFactory)9 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)9 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)9 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)9 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)9 Grammar (org.antlr.v4.tool.Grammar)9 LexerGrammar (org.antlr.v4.tool.LexerGrammar)9 STGroupString (org.stringtemplate.v4.STGroupString)9 LinkedHashMap (java.util.LinkedHashMap)7 URL (java.net.URL)6 Map (java.util.Map)6