Giter Club home page Giter Club logo

jsii-srcmak's Introduction

jsii-srcmak

Generates jsii source files for multiple languages from TypeScript.

Usage

This package can be either used as a library or through a CLI.

The library entry point is the srcmak function:

import { srcmak } from 'jsii-srcmak';
await srcmak(srcdir[, options]);

The CLI is jsii-srcmak:

$ jsii-srcmak srcdir [OPTIONS]

The srcdir argument points to a directory tree that includes TypeScript files which will be translated through jsii to one of the supported languages.

Compile only

If called with no additional arguments, srcmak will only jsii-compile the source. If compilation fails, it will throw an error. This is a nice way to check if generated typescript code is jsii-compatible:

const srcdir = generateSomeTypeScriptCode();

// verify it is jsii-compatible (throws otherwise)
await srcmak(srcdir);

CLI:

$ jsii-srcmak /source/directory

Python Output

To produce a Python module from your source, use the python option:

await srcmak('srcdir', {
  python: {
    outdir: '/path/to/project/root',
    moduleName: 'name.of.python.module'
  }
});

Or the --python-* switches in the CLI:

$ jsii-srcmak /src/dir --python-outdir=dir --python-module-name=module.name
  • The outdir/--python-outdir option points to the root directory of your Python project.
  • The moduleName/--python-module-name option is the python module name. Dots (.) delimit submodules.

The output directory will include a python module that corresponds to the original module. This code depends on the following python modules:

Java Output

To produce a Java module from your source, use the java option:

await srcmak('srcdir', {
  java: {
    outdir: '/path/to/project/root',
    package: 'hello.world'
  }
});

Or the --java-* switches in the CLI:

$ jsii-srcmak /src/dir --java-outdir=dir --java-package=hello.world
  • The outdir/--java-outdir option points to the root directory of your Java project.
  • The package/--java-package option is the java package name.

The output directory will include a java module that corresponds to the original module. This code depends on the following maven package (should be defined directly or indirectly in the project's pom.xml file):

The output directory will also include a tarball [email protected] that must be bundled in your project.

C# Output

To produce a C# module from your source, use the csharp option:

await srcmak('srcdir', {
  csharp: {
    outdir: '/path/to/project/root',
    namespace: 'HelloWorld'
  }
});

Or the --csharp-* switches in the CLI:

$ jsii-srcmak /src/dir --csharp-outdir=dir --csharp-namespace=HelloWorld
  • The outdir/--csharp-outdir option points to the root directory of your C# project.
  • The package/--csharp-namespace option is the C# root namespace.

The output directory will include a C# project that corresponds to the original module. This code depends on the following NuGet package (It is already defined as a dependency in the generated project):

The output directory will also include a tarball [email protected] that must be bundled in your project (It is already included as an embedded resource in the generated project).

Go Output

To produce a Go module from your source, use the golang option:

await srcmak('srcdir', {
  golang: {
    outdir: '/path/to/project/root',
    moduleName: 'github.com/yourorg/your-root-project',
    packageName: 'helloworld'
  }
});

Or the --golang-* switches in the CLI:

$ jsii-srcmak /src/dir --golang-outdir=dir --golang-module="github.com/yourorg/your-root-project" --golang-package="helloworld"
  • The outdir/--golang-outdir option points to the root directory of your base Go project (where your go.mod is in, if you have one).
  • The moduleName/--golang-module option must match the Go module name of the project that includes the generated source code e.g. github.com/yourorg/your-root-project. This is currently required, because the generated code needs to reference a submodule which is generated in a nested directory (see also upstream issue aws/jsii#2847 for more information).
  • The packageName/--golang-package is the package in which the generated Go code will be in. It will be placed in the submodule. So the import path becomes e.g. github.com/yourorg/your-root-project/yourpackage.

The output directory will include a directory named with the packageName/--golang-package containing the generated Go code. This code depends on the following Go module:

which you need to include in your go.mod:

require github.com/aws/jsii-runtime-go v1.29.0 # update the version to match the jsii version used in your version of jsii-srcmak

Nested output directories

It is also possible to set the outdir/--golang-outdir option to a nested directory inside your Go project. For example, if you want to nest the generated code in a directory called generated. In that case you need to append the subdirectory to the module name (e.g. github.com/yourorg/your-root-project/generated):

$ jsii-srcmak /src/dir --golang-outdir=~/projects/your-root-project/generated --golang-module="github.com/yourorg/your-root-project/generated" --golang-package="helloworld"

Your import path will then become e.g. github.com/yourorg/your-root-project/generated/yourpackage.

Entrypoint

The entrypoint option can be used to customize the name of the typescript entrypoint (default is index.ts).

For example, if the code's entry point is under /srcdir/foobar/lib/index.ts then I can specify:

await srcmak('/srcdir', {
  entrypoint: 'foobar/lib/index.ts'
});

Or through the CLI:

$ jsii-srcmak /srcdir --entrypoint lib/main.ts

Dependencies

The deps option can be used to specify a list of node module directories (must have a package.json file) which will be symlinked into the workspace when compiling your code.

This is required if your code references types from other modules.

Use this idiom to resolve a set of modules directories from the calling process:

const modules = [
  '@types/node', // commonly needed
  'foobar'       // a node module in *my* closure
];

const getModuleDir = m =>
  path.dirname(require.resolve(`${m}/package.json`));

await srcmak('srcdir', {
  deps: modules.map(getModuleDir)
});

Or through the CLI:

$ jsii-srcmak /src/dir --dep node_modules/@types/node --dep node_modules/constructs

Contributing

To build this project, you must first generate the package.json:

npx projen

Then you can install your dependencies and build:

yarn install
yarn build

What's with this name?

It's a silly little pun that stems from another pun: jsii has jsii-pacmak which stands for "package maker". That's the tool that takes in a .jsii manifest and produces language-idiomatic packages from it. This tool produces sources from a .jsii manifest. Hence, "source maker". Yeah, it's lame.

License

Distributed under the Apache 2.0 license.

jsii-srcmak's People

Contributors

ansgarm avatar aws-cdk-automation avatar campionfellin avatar cdklabs-automation avatar corymhall avatar danielmschmidt avatar dependabot[bot] avatar eladb avatar github-actions[bot] avatar iliapolo avatar jsteinich avatar mergify[bot] avatar mrarnoldpalmer avatar njlynch avatar otaviomacedo avatar romainmuller avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsii-srcmak's Issues

can not compile to java source file

"C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.3\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" H:\ts\ts2as\action.js
NOTE: Temp directory retained due to an error: C:\Users\JIANGY1\AppData\Local\Temp\temp-6BfOnA
(node:7708) UnhandledPromiseRejectionWarning: Error: jsii compilation failed with non-zero exit code: 1
| [2021-03-19T16:15:24.233] [ERROR] jsii/compiler - Type model errors prevented the JSII assembly from being created
| error JSII4: Could not find "main" file: C:\main.ts
| warning JSII3: There is no "README.md" file. It is required in order to generate valid PyPI (Python) packages.
+----------------------------------------------------------------------------------
| Command: H:\ts\ts2as\node_modules\jsii\bin\jsii --silence-warnings reserved-word
| Workdir: C:\Users\JIANGY
1\AppData\Local\Temp\temp-6BfOnA
+----------------------------------------------------------------------------------
at newError (H:\ts\ts2as\node_modules\jsii-srcmak\lib\util.js:38:20)
at ChildProcess.child.once.code (H:\ts\ts2as\node_modules\jsii-srcmak\lib\util.js:55:29)
at Object.onceWrapper (events.js:273:13)
at ChildProcess.emit (events.js:182:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
(node:7708) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7708) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Process finished with exit code 0

my project looks like:
image

os: win7
node: v10.15.0
npm:6.5.0

thanks!

Java support!

This library should be able to generate JSII source files for Java

Dependencies are always built with exact version contraints

Any dependencies are passed to jsii by reading the version out of the module's package.json file. This results in an exact version being required. This makes it more difficult to use the generated code with other libraries which may use a different version of the dependency.

Changing Options.dep to { [module: string]: string } would allow specifying a SemVar version for each module dependency. Could instead add a new option or union type to maintain compatibility.

Copy Java pom.xml to outdir

jsii-pacmak generates a pom.xml file alongside the generated java code src directory; however, jsii-srcmak only copies the src directory. That works out fine when putting the generated code side of application code (what cdk8s and cdktf do currently). That pom.xml would be needed (or another one) in order to place the generated code elsewhere and reference as a separate project. That would also align better with what the other supported languages are doing.
Could make it an option to prevent breaking existing uses.

JSII runs into OOM issues when spawned through jsii-srcmak

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

I was running into problems with jsii-srcmak on CI on terraform-cdk where it would go OOM. When debugging this we found that calling JSII directly on the same source ran with a third of the memory and no issues.

Here is the diff that solved the problem for me, I aligned the helper in jsii-pacmak:

diff --git a/node_modules/jsii-srcmak/lib/util.js b/node_modules/jsii-srcmak/lib/util.js
index f2cc7e4..69ca1a3 100644
--- a/node_modules/jsii-srcmak/lib/util.js
+++ b/node_modules/jsii-srcmak/lib/util.js
@@ -47,6 +47,8 @@ async function exec(moduleName, args = [], options = {}) {
         const opts = {
             ...options,
             stdio: ['inherit', 'pipe', 'pipe'],
+            shell: true,
+            env: { ...process.env }
         };
         const child = (0, child_process_1.spawn)(process.execPath, [moduleName, ...args], opts);
         const data = new Array();

Would you be open to a PR including this change?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.