88use MarekNocon \ComposerCheckout \PullRequest \GithubPullRequestData ;
99use Symfony \Component \Console \Input \ArrayInput ;
1010use Symfony \Component \Console \Input \InputInterface ;
11+ use Symfony \Component \Console \Input \InputOption ;
1112use Symfony \Component \Console \Output \OutputInterface ;
1213
1314class CheckoutCommand extends BaseCommand
@@ -16,20 +17,21 @@ protected function configure(): void
1617 {
1718 parent ::configure ();
1819 $ this ->setName ('checkout ' );
19- $ this ->setHelp (
20- 'Adds the branch from given GitHub Pull Request as a Composer dependency. '
21- );
20+ $ this ->setHelp ('Adds the branch from given GitHub Pull Request as a Composer dependency. ' );
21+ $ this ->addOption ('prefer-source ' , 's ' , InputOption::VALUE_OPTIONAL );
2222 }
2323
2424 protected function execute (InputInterface $ input , OutputInterface $ output ): int
2525 {
2626 $ pullRequestUrls = $ this ->validateInput ($ input ->getArgument ('pullRequestUrls ' ));
2727
28+ $ preferSource = $ input ->hasOption ('prefer-source ' );
29+
2830 foreach ($ pullRequestUrls as $ pullRequestUrl ) {
2931 $ githubPullRequestData = GithubPullRequestData::fromUrl ($ pullRequestUrl );
3032 $ composerPullRequestData = $ this ->extractDataFromPullRequest ($ githubPullRequestData );
3133 $ this ->addRepository ($ composerPullRequestData ->repositoryUrl , $ output );
32- $ this ->requireDependency ($ composerPullRequestData , $ output );
34+ $ this ->requireDependency ($ composerPullRequestData , $ output, $ preferSource );
3335 }
3436
3537 $ this ->executePostInstallCommands ($ output );
@@ -102,7 +104,7 @@ private function extractDataFromPullRequest(GithubPullRequestData $pullRequestDa
102104 );
103105 }
104106
105- private function requireDependency (ComposerPullRequestData $ pullRequestData , OutputInterface $ output ): void
107+ private function requireDependency (ComposerPullRequestData $ pullRequestData , OutputInterface $ output, bool $ preferSource ): void
106108 {
107109 /** @var RequireCommand */
108110 $ requireCommand = $ this ->getApplication ()->get ('require ' );
@@ -113,15 +115,19 @@ private function requireDependency(ComposerPullRequestData $pullRequestData, Out
113115 $ dependencyString
114116 ));
115117
116- $ input = new ArrayInput ([
117- 'packages ' => [
118- $ pullRequestData ->packageName ,
119- $ dependencyString ,
120- ],
121- '--no-scripts ' => true ,
122- ]);
118+ $ input = [
119+ 'packages ' => [
120+ $ pullRequestData ->packageName ,
121+ $ dependencyString ,
122+ ],
123+ '--no-scripts ' => true ,
124+ ];
125+
126+ if ($ preferSource ) {
127+ $ input ['--prefer-source ' ] = true ;
128+ }
123129
124- if ($ requireCommand ->run ($ input , $ output )) {
130+ if ($ requireCommand ->run (new ArrayInput ( $ input) , $ output )) {
125131 throw new \RuntimeException ('`Failed on adding dependency ' );
126132 }
127133 }
0 commit comments