예제 #1
0
 public void generate(string dstDir, ObjectValue model)
 {
     foreach (var fn in this.srcFiles)
     {
         var srcFn = $"{this.templateDir}/src/{fn}";
         var dstFn = $"{dstDir}/{fn}";
         if (this.meta.templateFiles.includes(fn))
         {
             var tmpl    = new TemplateParser(OneFile.readText(srcFn)).parse();
             var dstFile = tmpl.format(new VMContext(model));
             OneFile.writeText(dstFn, dstFile);
         }
         else
         {
             OneFile.copy(srcFn, dstFn);
         }
     }
 }
예제 #2
0
        public async Task generate()
        {
            // copy native source codes from one project
            var nativeSrcDir = $"{this.projDir}/{this.projectFile.nativeSourceDir}";

            foreach (var fn in OneFile.listFiles(nativeSrcDir, true))
            {
                OneFile.copy($"{nativeSrcDir}/{fn}", $"{this.outDir}/{fn}");
            }

            var generators = new IGenerator[] { ((IGenerator) new JavaGenerator()), ((IGenerator) new CsharpGenerator()), ((IGenerator) new PythonGenerator()), ((IGenerator) new PhpGenerator()) };

            foreach (var tmplName in this.projectFile.projectTemplates)
            {
                var compiler = await CompilerHelper.initProject(this.projectFile.name, this.srcDir, this.projectFile.sourceLang, null);

                compiler.processWorkspace();

                var projTemplate = new ProjectTemplate($"{this.baseDir}/project-templates/{tmplName}");
                var langId       = projTemplate.meta.language;
                var generator    = generators.find(x => x.getLangName().toLowerCase() == langId);
                var langName     = generator.getLangName();
                var outDir       = $"{this.outDir}/{langName}";

                foreach (var trans in generator.getTransforms())
                {
                    trans.visitFiles(Object.values(compiler.projectPkg.files));
                }

                // copy implementation native sources
                var oneDeps    = new List <ImplementationPackage>();
                var nativeDeps = new Dictionary <string, string> {
                };
                foreach (var dep in this.projectFile.dependencies)
                {
                    var impl = compiler.pacMan.implementationPkgs.find(x => x.content.id.name == dep.name);
                    oneDeps.push(impl);
                    var langData = impl.implementationYaml.languages.get(langId);
                    if (langData == null)
                    {
                        continue;
                    }

                    foreach (var natDep in langData.nativeDependencies ?? new ImplPkgNativeDependency[0])
                    {
                        nativeDeps.set(natDep.name, natDep.version);
                    }

                    if (langData.nativeSrcDir != null)
                    {
                        if (projTemplate.meta.packageDir == null)
                        {
                            throw new Error("Package directory is empty in project template!");
                        }
                        var srcDir   = langData.nativeSrcDir + (langData.nativeSrcDir.endsWith("/") ? "" : "/");
                        var dstDir   = $"{outDir}/{projTemplate.meta.packageDir}/{langData.packageDir ?? impl.content.id.name}";
                        var depFiles = Object.keys(impl.content.files).filter(x => x.startsWith(srcDir)).map(x => x.substr(srcDir.length()));
                        foreach (var fn in depFiles)
                        {
                            OneFile.writeText($"{dstDir}/{fn}", impl.content.files.get($"{srcDir}{fn}"));
                        }
                    }

                    if (langData.generatorPlugins != null)
                    {
                        foreach (var genPlugFn in langData.generatorPlugins)
                        {
                            generator.addPlugin(new TemplateFileGeneratorPlugin(generator, impl.content.files.get(genPlugFn)));
                        }
                    }
                }

                // generate cross compiled source code
                console.log($"Generating {langName} code...");
                var files = generator.generate(compiler.projectPkg);
                foreach (var file in files)
                {
                    OneFile.writeText($"{outDir}/{projTemplate.meta.destinationDir ?? ""}/{file.path}", file.content);
                }

                // generate files from project template
                var model = new ObjectValue(new Dictionary <string, IVMValue> {
                    ["dependencies"] = ((IVMValue) new ArrayValue(Object.keys(nativeDeps).map(name => new ObjectValue(new Dictionary <string, IVMValue> {
                        ["name"] = ((IVMValue) new StringValue(name)),
                        ["version"] = ((IVMValue) new StringValue(nativeDeps.get(name)))
                    })))),
                    ["onepackages"] = ((IVMValue) new ArrayValue(oneDeps.map(dep => new ObjectValue(new Dictionary <string, IVMValue> {
                        ["vendor"] = ((IVMValue) new StringValue(dep.implementationYaml.vendor)),
                        ["id"] = ((IVMValue) new StringValue(dep.implementationYaml.name))
                    }))))
                });
                projTemplate.generate($"{outDir}", model);
            }
        }