Skip to content

Conversation

@adamralph
Copy link
Contributor

fixes #990
fixes #897
fixes #945
fixes #962
fixes #963

@glennblock
Copy link
Contributor

Please wait on doing any big merges until the Script LIbrary PR goes in.

@glennblock glennblock added the hold label Mar 3, 2015
@adamralph adamralph mentioned this pull request Mar 4, 2015
@adamralph
Copy link
Contributor Author

Incidentally, -version and -help now execute instantly (previously each had a 2 second delay on my machine) since they are now effectively a no-op with regard to the application.

Help output is also much nicer, since the extra line breaks have gone away:

image

@adamralph
Copy link
Contributor Author

I rebased this after the script libraries changes went into dev (there were no conflicts)

@adamralph adamralph removed the hold label Mar 4, 2015
@adamralph
Copy link
Contributor Author

This is good to go now.

@adamralph
Copy link
Contributor Author

rebased, FWIW

@adamralph adamralph changed the title CLI args/config file refactoring #897 Rewrite of Program.Main, CLI args handling and config file reading #897 Mar 7, 2015
@adamralph
Copy link
Contributor Author

@scriptcs/core it would be great if someone could have a look at this today. I'm flying to Portugal tomorrow afternoon and I'll be largely dark for a week.

It's a big change set, but each commit is very focused so reviewing one commit at a time is probably going to be easiest and quickest.

I'd love to see this go in. It's a massive simplification of the entry point to the app.

@filipw
Copy link
Member

filipw commented Mar 7, 2015

does it handle stuf like scriptcs -install foo 4.0.5 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is it possible it now shows up without new lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've really no idea. It's a nice side effect of the PR though 😜

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adam I meant in a function not a sep class :-)
On Sat, Mar 7, 2015 at 4:20 PM Adam Ralph notifications@github.com wrote:

In src/ScriptCs/Program.cs
#944 (comment):

             return 1;
         }
  •        var scriptServicesBuilder = ScriptServicesBuilderFactory.Create(arguments.CommandArguments, arguments.ScriptArguments);
    
  •        var factory = new CommandFactory(scriptServicesBuilder);
    
  •        var command = factory.CreateCommand(arguments.CommandArguments, arguments.ScriptArguments);
    
  •        return (int)command.Execute();
    
  •    }
    
  •        if (commandArgs.Help)
    
  •        {
    
  •            Console.WriteLine(ScriptCsArgs.GetUsage());
    

I've really no idea. It's a nice side effect of the PR though [image:
😜]


Reply to this email directly or view it on GitHub
https://github.com/scriptcs/scriptcs/pull/944/files#r26000964.

@filipw
Copy link
Member

filipw commented Mar 7, 2015

does it handle starting REPL with log level set?

@filipw
Copy link
Member

filipw commented Mar 7, 2015

and a non-educated comment - you have deleted lots of tests; I understand the corresponding classes are gone, but do we no longer need to check the scenarios they covered?

@adamralph
Copy link
Contributor Author

Guys, thanks very much for reviewing! It was nice to come home this evening to all these comments.

In response:

@filipw

does it handle stuf like scriptcs -install foo 4.0.5 ?

Yes, that is handled inside ScriptCsArgs.Parse() and is unit tested.

does it handle starting REPL with log level set?

Yes, this has been handled in CommandFactory since #869 went out in 0.13. That code has not been changed.

and a non-educated comment - you have deleted lots of tests; I understand the corresponding classes are gone, but do we no longer need to check the scenarios they covered?

There is way less code now and it is much simpler. The work now takes place in three places:-

  • ScriptCsArgs.Parse() - the only meaningful thing that happens inside here beside wrapping around PowerArgs.Args.Parse<T>() is the special casing for -install to allow it to take a package name and a version. This is unit tested in ScriptCsArgsTests.
  • ConfigMask a trivial type with a static factory method and a File.Exists wrapper around a JSON.NET deserialization call
  • Config.Apply() applies a ConfigMask to a Config and returns a new Config

I think all but one of the scenarios which these types affect are covered in the acceptance tests. The only one which isn't, is the masking of CLI args over local and global options files and, come to think of it, that acceptance test should probably be added. My general approach to testing is to ensure the important scenarios are covered by acceptance tests and only to drill down into 'unit tests' when it's felt some particular implementation detail is risky enough to require it. That's why I included a unit test for ScriptCsArgs.Parse(). This is really the only complicated bit of logic that has survived from the original implementation, the rest is rather trivial.

@glennblock

This block of code that parses args should be in its own function and called by main. Makes the code easier to maintain.

Personally I would leave it where it is since it's trivial and single use. I only tend to encapsulate something if it's complicated or reused. If feelings are strong on this, I can encapsulate in a function.

@adamralph
Copy link
Contributor Author

@glennblock sure, I meant function too, 'encapsulate' was the wrong word 😄

So to paraphrase what Main is doing:

  • divide the args into those for me and those for the script
  • read the args intended for me (if I can't understand them, show what I do understand and return an error code)
  • if you want help, show it and return a success code
  • if you want to know my version, show it and return a success code
  • if you want to load a config file which doesn't exist, refuse and return an error code
  • create a config based on what I've been told
  • load configs from the global and local files and mask the local config over the global config and my config over that
  • create a command factory, feed the config into it and execute any resulting commands

Which part(s) do you think should move to a function?

@glennblock
Copy link
Contributor

Lines 18 to 35, everything related to parsing the args
On Sat, Mar 7, 2015 at 5:21 PM Adam Ralph notifications@github.com wrote:

@glennblock https://github.com/glennblock sure, I meant function too,
'encapsulate' was the wrong word [image: 😄]

So to paraphrase what Main is doing:

  • divide the args into those for me and those for the script
  • read the args for me (if I can't understand them, show what I do
    understand and return an error code)
  • if you want help, show it and return a success code
  • if you want to know my version, show it and return a success code
  • if you want to load a config file which doesn't exist, refuse and
    return an error code
  • create a config based on what I've been told
  • load configs from the global and local files and mask the local
    config over the global config and my config over that
  • create a command factory, feed the config into it and execute any
    resulting commands

Which part(s) do you think should move to a function?


Reply to this email directly or view it on GitHub
#944 (comment).

@adamralph
Copy link
Contributor Author

Parsing the args is already encapsulated in ScriptCsArgs.Parse() (second point above).

Tell you what, if this is the only thing you'd like to change, perhaps you can merge this PR and send your proposed refactoring in a new PR?

@adamralph
Copy link
Contributor Author

@filipw I've added Configuration scenarios to the acceptance tests

@glennblock
Copy link
Contributor

I am not finished reviewing. As to the refactoring it is reducing the code in Main and making it more readable. Extract Method is a very common pattern. The lines I mentioned can be easily ripped to a parse method.

@adamralph
Copy link
Contributor Author

Sure, I'm familiar with Extract Method 😉.

I guess it's a matter of taste. IMHO the lines should stay where they are. The key aspect is the return codes. There are a few cases in the method which Main needs to check and return a 0 or 1 appropriately. I wouldn't move these elsewhere as I think that will make the code less readable, since you'd have to propagate the return codes back up the stack.

There is actually only one line which does arg parsing, and that is line 22. The heavy lifting there is already extracted into ScriptCsArgs.Parse().

The only lines which I think could be moved elsewhere are:

  • 16 and 17, but this would come with the overhead of either creating a return type, returning a Tuple<T1, T2> or using out parameters. All of these options I think would bloat the code for little benefit
  • lines 49 to 52 - this is the loading and masking of the local and global config. Although it's 4 lines, it's a single statement with method chaining and I think it's very easy to read so I'd probably leave as is.

@adamralph
Copy link
Contributor Author

Ultimately Main is only 46 lines long and, as the entry point, it needs to orchestrate according to the CLI args with the constraint of returning status codes. In that context, I think it's as decomposed as is practical.

@adamralph adamralph mentioned this pull request Mar 23, 2015
@adamralph adamralph added the hold label Mar 23, 2015
@adamralph
Copy link
Contributor Author

hold until post 0.14

@adamralph adamralph removed the hold label Mar 23, 2015
@adamralph
Copy link
Contributor Author

0.14 has been released, so this PR is good to go again.

@adamralph adamralph added the hold label Mar 24, 2015
@adamralph
Copy link
Contributor Author

hold off - see #990

@adamralph adamralph self-assigned this Mar 24, 2015
@adamralph
Copy link
Contributor Author

@glennblock Program.Main is now smaller 😉

@adamralph adamralph removed the hold label Mar 25, 2015
@adamralph
Copy link
Contributor Author

This is good to go again.

@adamralph adamralph removed their assignment Mar 25, 2015
@filipw
Copy link
Member

filipw commented Mar 25, 2015

:shipit:

@adamralph adamralph mentioned this pull request Mar 25, 2015
29 tasks
glennblock added a commit that referenced this pull request Mar 27, 2015
Rewrite of Program.Main, CLI args handling and config file reading #897
@glennblock glennblock merged commit d9e042c into scriptcs:dev Mar 27, 2015
@glennblock
Copy link
Contributor

👍

Great work, much more simplified!

@adamralph adamralph deleted the 897 branch March 27, 2015 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

3 participants