Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: move code out of tool.go #1103

Open
paralin opened this issue Feb 7, 2022 · 2 comments
Open

Proposal: move code out of tool.go #1103

paralin opened this issue Feb 7, 2022 · 2 comments

Comments

@paralin
Copy link
Contributor

paralin commented Feb 7, 2022

Right now there is a lot of code in tool.go, defined under in-line functions.

Ideally this code would be broken out into packages accessible when using Gopherjs as a library:

	sess, err := gbuild.NewSession(&opts)
	if err != nil {
		return err
	}

	xctx := gbuild.NewBuildContext(sess.InstallSuffix(), opts.BuildTags)
	pkgs, err := xctx.Match(args)

^^ These steps :)

@nevkontakte
Copy link
Member

Yes, a lot of what's currently in tool.go should really be in the build package. In theory, you can already do a lot with the build package, though beware that breaking changes to that package API can and will happen :)

@paralin
Copy link
Contributor Author

paralin commented Feb 9, 2022

Indeed, this is my build script:

	// list of packages to build
	args := []string{"github.com/my/project"}

	// build the js
	runtimeOut := path.Join(runtimeDir, "runtime-gopherjs.js")

	// hack: disable version check
	os.Setenv("GOPHERJS_SKIP_VERSION_CHECK", "true")

	opts := BuildOptions
	opts.GOROOT = runtime.GOROOT()
	opts.GOPATH = ""

	sess, err := gbuild.NewSession(&opts)
	if err != nil {
		return err
	}

	xctx := gbuild.NewBuildContext(sess.InstallSuffix(), opts.BuildTags)
	pkgs, err := xctx.Match(args)
	if err != nil {
		return errors.Wrapf(err, "failed to expand patterns: %v", args)
	}
	if len(pkgs) != 1 {
		return errors.Errorf("expected %d packages but got %d", 1, len(pkgs))
	}

	pkgPath := pkgs[0]
	pkg, err := xctx.Import(pkgPath, repoRoot, 0)
	if err != nil {
		return err
	}
	if !pkg.IsCommand() {
		return errors.Errorf("expected package with main function: %s", pkg.ImportPath)
	}

	ar, err := sess.BuildPackage(pkg)
	if err != nil {
		return err
	}

	err = sess.WriteCommandPackage(ar, runtimeOut)
	if err != nil {
		return err
	}

	return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants