jozefizso / generator-license

Yeoman Generator - License
MIT License
59 stars 20 forks source link

Getting the selected license as a return parameter? #37

Closed razzeee closed 7 years ago

razzeee commented 8 years ago

Is it somehow possible to get the selected license/licenseId as a return parameter?

SBoudrias commented 7 years ago

@Razzeee you can get it from the generated package.json license field.

this.fs.readJSON(this.destinationPath('package.json')).license;
razzeee commented 7 years ago

Unfortunatly I need it before I write the files, as I need to write it to one of the files I'm writing it too. Not sure how to do it. My code is here: https://github.com/Razzeee/generator-kodi/blob/master/generators/app/index.js

ek9 commented 7 years ago

@Razzeee you use this.props.license after the prompt has been answered it will be populated with value from licenses (so it will be MIT or Apache-2.0 or GPL-3.0 etc.).

You can see how filename is generated by appending .txt to what you chose on https://github.com/jozefizso/generator-license/blob/master/app/index.js#L100

Hope this helps.

razzeee commented 7 years ago

@ek9 the variable doesn't seem to be filed, probably due to the scope being a different one

SBoudrias commented 7 years ago

Yeoman generators run in isolation. They don't return values.

The only way to get the selected license is from reading the value from the package.json.

You should read about the run loop here http://yeoman.io/authoring/running-context.html and order your tasks in a way where you'll be able to grab the license value from package.json to add it elsewhere.

razzeee commented 7 years ago

@SBoudrias so your saying that I need a package.json to solve this? I'm creating python code, so I'm not creating a package.json

SBoudrias commented 7 years ago

@Razzeee so you want the license to include in setup.py?

Maybe we could add support for adding the license inside setup.py directly in this generator.

razzeee commented 7 years ago

nope, I want to add it to a addon.xml as it's a generator for a kodi addon :/

ek9 commented 7 years ago

Well adding things to addon.xml isn't part of this package's scope. What I believe you should be doing:

  1. Show the prompt in your generator, where user can pick license
  2. Use this generator as a sub generator and pass the license as an option, so this generator uses it.

However, this is likely to require some updates to this package too, as even when provided with some options, it still asks you to confirm things or would provide the questions multiple times. To avoid this, there needs to be some updating on when certain prompts are shown.

I plan to play around with some generators later this week, and wanted to use this one as sub-generator. Perhaps I can answer in more detail later or provide a PR which would make it possible to implement it with this.

razzeee commented 7 years ago

Well could I just have my own license list and just add a matching license to options when running composeWith? If the generator does the rest of work needed, I'm fine. Will try and update later.

ek9 commented 7 years ago

You can find an example on how generator-node uses this module when asking which license to pick

https://github.com/yeoman/generator-node/blob/master/generators/app/index.js#L277

This also happens if you run yo generator (need generator-generator installed for that) as it calls generator-node::app which in turn calls generator-license::app)

The PR that is needed for you need is ability to provide "license" as an option (same way you can do with name etc.), so the license can be skipped. That way, you can have prompt in your own generator and only supply a license value to this generator. Otherwise, it will ask you for license 2nd time.

Also useful documentation on composability of Yeoman Generators: http://yeoman.io/authoring/composability.html

SBoudrias commented 7 years ago

require('generator-license').licenses is the list of license this generator supports. So you can use that.

ek9 commented 7 years ago

I believe this one can be closed now. It might not be too bad idea to add the above comment by @SBoudrias to README.md as it's useful information when composing generators with this one.