diff --git a/.gitignore b/.gitignore index 56ed8076c..fc9e26d09 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ phpunit.xml /Homestead.yaml /Homestead.json +/before.sh /after.sh /aliases /mysqldump.sql.gz diff --git a/Vagrantfile b/Vagrantfile index 2d94f5282..c237766ac 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -9,6 +9,7 @@ confDir = $confDir ||= File.expand_path(File.dirname(__FILE__)) homesteadYamlPath = confDir + "/Homestead.yaml" homesteadJsonPath = confDir + "/Homestead.json" +beforeScriptPath = confDir + "/before.sh" afterScriptPath = confDir + "/after.sh" customizationScriptPath = confDir + "/user-customizations.sh" aliasesPath = confDir + "/aliases" @@ -25,6 +26,10 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| end end + if File.exist? beforeScriptPath then + config.vm.provision "Run before.sh", type: "shell", path: beforeScriptPath, keep_color: true + end + if File.exist? homesteadYamlPath then settings = YAML::load(File.read(homesteadYamlPath)) elsif File.exist? homesteadJsonPath then diff --git a/init.bat b/init.bat index 62b30f3af..170c6d2a4 100644 --- a/init.bat +++ b/init.bat @@ -7,6 +7,7 @@ if ["%~1"]==[""] ( copy /-y resources\Homestead.yaml Homestead.yaml ) +copy /-y resources\before.sh before.sh copy /-y resources\after.sh after.sh copy /-y resources\aliases aliases diff --git a/init.sh b/init.sh index f5a66aed8..49e0f2b41 100644 --- a/init.sh +++ b/init.sh @@ -6,6 +6,7 @@ else cp -i resources/Homestead.yaml Homestead.yaml fi +cp -i resources/before.sh before.sh cp -i resources/after.sh after.sh cp -i resources/aliases aliases diff --git a/resources/before.sh b/resources/before.sh new file mode 100644 index 000000000..e582f4e70 --- /dev/null +++ b/resources/before.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +# If you would like to do some extra provisioning you may +# add any commands you wish to this file and they will +# be run before the Homestead machine is configured. +# +# If you have user-specific configurations you would liketo apply, +# you may also create user-customizations-before.sh, +# which will be run after this script. + + +# If you'd like to use local mirrors instead of the default US mirrors +# uncomment these lines to add these to your sources.list + +#cp /etc/apt/sources.list /etc/apt/sources.list.backup + +#cat <addOption('name', null, InputOption::VALUE_OPTIONAL, 'The name of the virtual machine.', $this->defaultProjectName) ->addOption('hostname', null, InputOption::VALUE_OPTIONAL, 'The hostname of the virtual machine.', $this->defaultProjectName) ->addOption('ip', null, InputOption::VALUE_OPTIONAL, 'The IP address of the virtual machine.') + ->addOption('no-before', null, InputOption::VALUE_NONE, 'Determines if the before.sh file is not created.') ->addOption('no-after', null, InputOption::VALUE_NONE, 'Determines if the after.sh file is not created.') ->addOption('no-aliases', null, InputOption::VALUE_NONE, 'Determines if the aliases file is not created.') ->addOption('example', null, InputOption::VALUE_NONE, 'Determines if a Homestead example file is created.') @@ -75,6 +76,10 @@ public function execute(InputInterface $input, OutputInterface $output) $this->createAliasesFile(); } + if (! $input->getOption('no-before') && ! $this->beforeShellScriptExists()) { + $this->createBeforeShellScript(); + } + if (! $input->getOption('no-after') && ! $this->afterShellScriptExists()) { $this->createAfterShellScript(); } @@ -154,6 +159,16 @@ protected function createAliasesFile() } } + /** + * Determine if the after shell script exists. + * + * @return bool + */ + protected function beforeShellScriptExists() + { + return file_exists("{$this->basePath}/before.sh"); + } + /** * Determine if the after shell script exists. * @@ -164,6 +179,16 @@ protected function afterShellScriptExists() return file_exists("{$this->basePath}/after.sh"); } + /** + * Create the after shell script. + * + * @return void + */ + protected function createBeforeShellScript() + { + copy(__DIR__.'/../resources/before.sh', "{$this->basePath}/before.sh"); + } + /** * Create the after shell script. * diff --git a/tests/InitScriptTest.php b/tests/InitScriptTest.php index 977fc8890..5f7fc89ce 100644 --- a/tests/InitScriptTest.php +++ b/tests/InitScriptTest.php @@ -44,6 +44,14 @@ public function it_creates_a_homestead_json_file_if_requested() $this->assertFileExists(self::$testDirectory.'/Homestead.json'); } + /** @test */ + public function it_creates_an_before_shell_script() + { + exec('bash init.sh'); + + $this->assertFileExists(self::$testDirectory.'/before.sh'); + } + /** @test */ public function it_creates_an_after_shell_script() { diff --git a/tests/MakeCommandTest.php b/tests/MakeCommandTest.php index 46d834d41..6b80fe85e 100644 --- a/tests/MakeCommandTest.php +++ b/tests/MakeCommandTest.php @@ -129,6 +129,52 @@ public function an_aliases_file_is_not_created_if_it_is_explicitly_told_to() $this->assertFileDoesNotExist(self::$testDirectory.DIRECTORY_SEPARATOR.'aliases'); } + /** @test */ + public function an_before_shell_script_is_created_by_default() + { + $tester = new CommandTester(new MakeCommand()); + + $tester->execute([]); + + $this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'before.sh'); + + $this->assertFileEquals( + __DIR__.'/../resources/before.sh', + self::$testDirectory.DIRECTORY_SEPARATOR.'before.sh' + ); + } + + /** @test */ + public function an_existing_before_shell_script_is_not_overwritten() + { + file_put_contents( + self::$testDirectory.DIRECTORY_SEPARATOR.'before.sh', + 'Already existing before.sh' + ); + $tester = new CommandTester(new MakeCommand()); + + $tester->execute([]); + + $this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'before.sh'); + + $this->assertStringEqualsFile( + self::$testDirectory.DIRECTORY_SEPARATOR.'before.sh', + 'Already existing before.sh' + ); + } + + /** @test */ + public function an_before_file_is_not_created_if_it_is_explicitly_told_to() + { + $tester = new CommandTester(new MakeCommand()); + + $tester->execute([ + '--no-before' => true, + ]); + + $this->assertFileDoesNotExist(self::$testDirectory.DIRECTORY_SEPARATOR.'before.sh'); + } + /** @test */ public function an_after_shell_script_is_created_by_default() {