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

before.sh #1917

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
phpunit.xml
/Homestead.yaml
/Homestead.json
/before.sh
/after.sh
/aliases
/mysqldump.sql.gz
Expand Down
5 changes: 5 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions init.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions resources/before.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF | tee /etc/apt/sources.list
#deb mirror://mirrors.ubuntu.com/mirrors.txt $(lsb_release -cs) main restricted universe multiverse
#deb mirror://mirrors.ubuntu.com/mirrors.txt $(lsb_release -cs)-updates main restricted universe multiverse
#deb mirror://mirrors.ubuntu.com/mirrors.txt $(lsb_release -cs)-backports main restricted universe multiverse
#deb mirror://mirrors.ubuntu.com/mirrors.txt $(lsb_release -cs)-security main restricted universe multiverse
#EOF

#apt-get update -y
5 changes: 5 additions & 0 deletions resources/localized/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ confDir = $confDir ||= File.expand_path("vendor/laravel/homestead", File.dirname

homesteadYamlPath = File.expand_path("Homestead.yaml", File.dirname(__FILE__))
homesteadJsonPath = File.expand_path("Homestead.json", File.dirname(__FILE__))
beforeScriptPath = "before.sh"
afterScriptPath = "after.sh"
customizationScriptPath = "user-customizations.sh"
aliasesPath = "aliases"
Expand All @@ -25,6 +26,10 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
end
end

if File.exist? beforeScriptPath then
config.vm.provision "shell", path: beforeScriptPath, keep_color: true
end

if File.exist? homesteadYamlPath then
settings = YAML::load(File.read(homesteadYamlPath))
elsif File.exist? homesteadJsonPath then
Expand Down
25 changes: 25 additions & 0 deletions src/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected function configure()
->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.')
Expand All @@ -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();
}
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/InitScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
46 changes: 46 additions & 0 deletions tests/MakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down