public static async Task <Compiler> initProject(string projectName, string sourceDir, string lang = "ts", string packagesDir = null) { if (lang != "ts") { throw new Error("Only typescript is supported."); } var compiler = new Compiler(); await compiler.init(packagesDir ?? $"{CompilerHelper.baseDir}packages/"); compiler.setupNativeResolver(OneFile.readText($"{CompilerHelper.baseDir}langs/NativeResolvers/typescript.ts")); compiler.newWorkspace(projectName); foreach (var file in OneFile.listFiles(sourceDir, true).filter(x => x.endsWith(".ts"))) { compiler.addProjectFile(file, OneFile.readText($"{sourceDir}/{file}")); } return(compiler); }
public async Task <PackageBundle> getAllCached() { var packages = new Dictionary <string, PackageContent> { }; var allFiles = OneFile.listFiles(this.packagesDir, true); foreach (var fn in allFiles) { var pathParts = fn.split(new RegExp("/")).ToList(); // [0]=implementations/interfaces, [1]=package-name, [2:]=path if (pathParts.length() < 3) { continue; } var type = pathParts.shift(); var pkgDir = pathParts.shift(); if (type != "implementations" && type != "interfaces") { continue; } // skip e.g. bundle.json var pkgIdStr = $"{type}/{pkgDir}"; var pkg = packages.get(pkgIdStr); if (pkg == null) { var pkgDirParts = pkgDir.split(new RegExp("-")).ToList(); var version = pkgDirParts.pop().replace(new RegExp("^v"), ""); var pkgType = type == "implementations" ? PackageType.Implementation : PackageType.Interface; var pkgId = new PackageId(pkgType, pkgDirParts.join("-"), version); pkg = new PackageContent(pkgId, new Dictionary <string, string> { }, true); packages.set(pkgIdStr, pkg); } pkg.files.set(pathParts.join("/"), OneFile.readText($"{this.packagesDir}/{fn}")); } return(new PackageBundle(Object.values(packages))); }
public ProjectTemplate(string templateDir) { this.templateDir = templateDir; this.meta = ProjectTemplateMeta.fromYaml(OneYaml.load(OneFile.readText($"{templateDir}/index.yaml"))); this.srcFiles = OneFile.listFiles($"{templateDir}/src", true); }
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); } }
public void listXCompiledNativeSources() { console.log(OneFile.listFiles($"{this.baseDir}/xcompiled-src/native", true).join("\n")); }