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

[Feature Request] Support call external commands as transformation #98

Open
hcoona opened this issue Sep 11, 2019 · 2 comments
Open

[Feature Request] Support call external commands as transformation #98

hcoona opened this issue Sep 11, 2019 · 2 comments

Comments

@hcoona
Copy link

hcoona commented Sep 11, 2019

I'd like to run code formatting in a copybara workflow, which needs to call external commands as transformation.

@denian
Copy link
Contributor

denian commented Sep 11, 2019

Hi Shuai,

The general approach that we've been following is to implement specific transformations (that can delegate on another tool/sub-process) instead of a general "black-box" transform where you can run any command.

There are several reasons for this, for example it provides a much better API in terms of the configuration. If you see a transform like (just making this up):

python.format(paths = glob(['**.py']),

it's much clear and scoped than something like:

core.run_binary('path/to/some/script.sh arg1 arg2...'),

and there's also the problem of, how do you specify the binary, how it's resolved, etc.

But the main issue is reversibility. Having a blind transform that runs some command does not offer any control on that, unless you make this transform explicitly non-reversible, or you have a reverse field.

We do have some format transformations internally, and they are implemented in this way:

public class MyFormatToolTransform implements Transformation {

  MyFormatToolTransform(Glob glob, MyFormatToolOptions formatOptions) {
    ...
  }

  @Override
  public void transform(TransformWork work) throws IOException, ValidationException {
    Iterable<FileState> files = work.getTreeState().find(glob.relativeTo(work.getCheckoutDir()));
    // Run your format tool on the files
    formatOptions.run(files);
    ...
  }

  @Override
  public Transformation reverse() {
    return new ExplicitReversal(IntentionalNoop.INSTANCE, this);
  }
}

where the MyFormatToolOptions resolves the binary to a default location (for example /usr/bin/format) but also exposes a flag so users can overwrite that.

Note that in this case we set explicitly that the format transformation reverse is noop.

Could you say more about the format tool that you want to run? Is it open source?

Also, are happy to accept contributions :)

@hcoona
Copy link
Author

hcoona commented Sep 16, 2019

I want support for:

Would you like to give more details about MyFormatToolTransform? I'd like to take it as an example for contributing.

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