Skip to content

Commit 0331aeb

Browse files
authored
Merge pull request #6 from canabadyweb/master
Used DIRECTORY_SEPARATOR for OS Path
2 parents 204c1e3 + 6e0d655 commit 0331aeb

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

GenerateCommand.php

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class GenerateCommand extends Command
1010
{
1111
private $dimensions = [], $flavors = [], $modules = ['app'];
1212
private $input, $output, $projectName, $pkgName;
13+
// private $pathSeparator;
1314

1415
protected function configure()
1516
{
@@ -45,6 +46,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
4546
{
4647
$this->input = $input;
4748
$this->output = $output;
49+
/*
50+
if ( php_uname("s") === "Linux" )
51+
{
52+
$this->pathSeparator = '/';
53+
}
54+
else
55+
{
56+
$this->pathSeparator = '\\';
57+
}
58+
*/
4859

4960
$this->projectName = $input->getArgument('project');
5061
$this->pkgName = $input->getArgument('pkg');
@@ -66,14 +77,14 @@ private function createProject()
6677
}
6778

6879
$this->_mkdir($this->projectName);
69-
$this->copyr(__DIR__ . '/stubs/gradle', $this->projectName . '\gradle');
70-
copy(__DIR__ . '/stubs/gradlew', $this->projectName . '\gradlew');
71-
copy(__DIR__ . '/stubs/gradlew.bat', $this->projectName . '\gradlew.bat');
72-
$this->_mkdir($this->projectName . '\build');
80+
$this->copyr(__DIR__ . '/stubs/gradle', $this->projectName . DIRECTORY_SEPARATOR . 'gradle');
81+
copy(__DIR__ . '/stubs/gradlew', $this->projectName . DIRECTORY_SEPARATOR . 'gradlew');
82+
copy(__DIR__ . '/stubs/gradlew.bat', $this->projectName . DIRECTORY_SEPARATOR . 'gradlew.bat');
83+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . 'build');
7384

74-
file_put_contents($this->projectName . '\build.gradle', "buildscript {\r\n\trepositories {\r\n\t\tgoogle()\r\n\t\tjcenter()\r\n\t}\r\n\tdependencies {\r\n\t\tclasspath 'com.android.tools.build:gradle:3.5.1'\r\n\t}\r\n}\r\n\nallprojects {\r\n\trepositories {\r\n\t\tgoogle()\r\n\t\tjcenter()\r\n\t}\r\n}\r\n\ntask clean(type: Delete) {\r\n\tdelete rootProject.buildDir\r\n}\r\n", 0);
75-
file_put_contents($this->projectName . '\.gitignore', "*.iml\r\n.gradle\r\n/local.properties\r\n/.idea/caches\r\n/.idea/libraries\r\n/.idea/modules.xml\r\n/.idea/workspace.xml\r\n/.idea/navEditor.xml\r\n/.idea/assetWizardSettings.xml\r\n.DS_Store\r\n/build\r\n/captures\r\n.externalNativeBuild\r\n/backup\r\n/priv", 0);
76-
file_put_contents($this->projectName . '\gradle.properties', "org.gradle.jvmargs=-Xmx1536m\r\nandroid.useAndroidX=" . ($this->input->getOption('androidX') ? "true" : "false") . "\r\nandroid.enableJetifier=" . ($this->input->getOption('jetifier') ? "true" : "false") . "\r\n", 0);
85+
file_put_contents($this->projectName . DIRECTORY_SEPARATOR . 'build.gradle', "buildscript {\r\n\trepositories {\r\n\t\tgoogle()\r\n\t\tjcenter()\r\n\t}\r\n\tdependencies {\r\n\t\tclasspath 'com.android.tools.build:gradle:3.5.1'\r\n\t}\r\n}\r\n\nallprojects {\r\n\trepositories {\r\n\t\tgoogle()\r\n\t\tjcenter()\r\n\t}\r\n}\r\n\ntask clean(type: Delete) {\r\n\tdelete rootProject.buildDir\r\n}\r\n", 0);
86+
file_put_contents($this->projectName . DIRECTORY_SEPARATOR . '.gitignore', "*.iml\r\n.gradle\r\n/local.properties\r\n/.idea/caches\r\n/.idea/libraries\r\n/.idea/modules.xml\r\n/.idea/workspace.xml\r\n/.idea/navEditor.xml\r\n/.idea/assetWizardSettings.xml\r\n.DS_Store\r\n/build\r\n/captures\r\n.externalNativeBuild\r\n/backup\r\n/priv", 0);
87+
file_put_contents($this->projectName . DIRECTORY_SEPARATOR . 'gradle.properties', "org.gradle.jvmargs=-Xmx1536m\r\nandroid.useAndroidX=" . ($this->input->getOption('androidX') ? "true" : "false") . "\r\nandroid.enableJetifier=" . ($this->input->getOption('jetifier') ? "true" : "false") . "\r\n", 0);
7788

7889
$settingsContent = "include ";
7990

@@ -84,9 +95,9 @@ private function createProject()
8495
$moduleType = isset($exModule[1]) ? $exModule[1] : 'application';
8596
}
8697

87-
$this->_mkdir($this->projectName . '\\' . $moduleName);
98+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName);
8899

89-
file_put_contents($this->projectName . '\\' . $moduleName . '\.gitignore', "/build\n\n", 0);
100+
file_put_contents($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . '.gitignore', "/build\n\n", 0);
90101

91102
$isAndroidX = $this->input->getOption('androidX');
92103

@@ -116,7 +127,7 @@ private function createProject()
116127
$fdContent = $dimensionContent . "\r\n\t" . $flavorContent;
117128

118129
file_put_contents(
119-
$this->projectName . '\\' . $moduleName . '\build.gradle',
130+
$this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'build.gradle',
120131
"apply plugin: 'com.android." . $moduleType . "'\r\n\n" .
121132
"android {\r\n\t" .
122133
"compileSdkVersion " . $this->input->getOption('compileSdk') . "\r\n\t" .
@@ -144,19 +155,19 @@ private function createProject()
144155
"}",
145156
0
146157
);
147-
file_put_contents($this->projectName . '\\' . $moduleName . '\proguard-rules.pro', "\r\n", 0);
158+
file_put_contents($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'proguard-rules.pro', "\r\n", 0);
148159

149-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\src');
150-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\build');
151-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\libs');
160+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src');
161+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'build');
162+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'libs');
152163

153-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\src\androidTest');
154-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\src\test');
155-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\src\main');
164+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'androidTest');
165+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'test');
166+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main');
156167

157168
if ($moduleType === 'application') {
158169
file_put_contents(
159-
$this->projectName . '\\' . $moduleName . '\src\main\AndroidManifest.xml',
170+
$this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'AndroidManifest.xml',
160171
'<?xml version="1.0" encoding="utf-8"?>
161172
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
162173
package="' . $this->pkgName . '">
@@ -182,7 +193,7 @@ private function createProject()
182193
);
183194
} else {
184195
file_put_contents(
185-
$this->projectName . '\\' . $moduleName . '\src\main\AndroidManifest.xml',
196+
$this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'AndroidManifest.xml',
186197
'<manifest xmlns:android="http://schemas.android.com/apk/res/android" ' . "\r\n\t" . 'package="' . $this->pkgName . ('app' === $moduleName ? '' : '.' . $moduleName) . '">' . "\r\n\n\t" . ($moduleType === 'library' ? '' : '<application' . "\r\n\t\t" .
187198
'android:allowBackup="true"' . "\r\n\t\t" .
188199
'android:icon="@mipmap/ic_launcher"' . "\r\n\t\t" .
@@ -195,28 +206,28 @@ private function createProject()
195206
0
196207
);
197208
}
198-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\src\main\java');
209+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'java');
199210
$exPkg = explode('.', $this->pkgName);
200-
$_dir = $this->projectName . '\\' . $moduleName . '\src\main\java';
211+
$_dir = $this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'java';
201212
$_newDir = '';
202213
foreach ($exPkg as $key => $pkgDir) {
203-
$_newDir .= '\\' . $pkgDir;
214+
$_newDir .= DIRECTORY_SEPARATOR . $pkgDir;
204215
$this->_mkdir($_dir . $_newDir);
205216
if ($key === (count($exPkg) - 1) && $moduleName !== 'app') {
206-
$this->_mkdir($_dir . $_newDir . '\\' . $moduleName);
217+
$this->_mkdir($_dir . $_newDir . DIRECTORY_SEPARATOR . $moduleName);
207218
}
208219
}
209220

210221
if ($moduleType === 'application') {
211222

212223
// Activity Java File Path Bug Debug
213224
// $this->_print_info('Package Name : ' . $this->pkgName);
214-
// $this->_print_info('Replaced Package Name : ' . str_replace('.', '\\',$this->pkgName));
215-
// $this->output->writeln('<info>MainActivity.java Path : ' . $this->projectName . '\\' . $moduleName . '\src\main\java\\' . str_replace('.', '\\',$this->pkgName) . '\\MainActivity.java' . '</info>');
225+
// $this->_print_info('Replaced Package Name : ' . str_replace('.', DIRECTORY_SEPARATOR,$this->pkgName));
226+
// $this->output->writeln('<info>MainActivity.java Path : ' . $this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'java'. DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR,$this->pkgName) . DIRECTORY_SEPARATOR . 'MainActivity.java' . '</info>');
216227

217228
// HelloWorld Activity
218229
file_put_contents(
219-
$this->projectName . '\\' . $moduleName . '\src\main\java\\' . str_replace('.', '\\', $this->pkgName) . '\\MainActivity.java',
230+
$this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'java'. DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR, $this->pkgName) . DIRECTORY_SEPARATOR . 'MainActivity.java',
220231
"package $this->pkgName;
221232
222233
import androidx.appcompat.app.AppCompatActivity;
@@ -236,17 +247,17 @@ private function createProject()
236247
}
237248

238249
if ($moduleType === 'library') {
239-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\src\main\res');
240-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\src\main\res\drawable');
241-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\src\main\res\layout');
242-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\src\main\res\values');
250+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'res');
251+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'res' . DIRECTORY_SEPARATOR . 'drawable');
252+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'res' . DIRECTORY_SEPARATOR . 'layout');
253+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'res' . DIRECTORY_SEPARATOR . 'values');
243254
} else {
244-
$this->copyr(__DIR__ . '/stubs/res', $this->projectName . '\\' . $moduleName . '\src\main\res');
255+
$this->copyr(__DIR__ . '/stubs/res', $this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'res');
245256

246257
// HelloWorld Layout
247-
$this->_mkdir($this->projectName . '\\' . $moduleName . '\src\main\res\layout');
258+
$this->_mkdir($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'res' . DIRECTORY_SEPARATOR . 'layout');
248259
file_put_contents(
249-
$this->projectName . '\\' . $moduleName . '\src\main\res\layout\activity_main.xml',
260+
$this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'res' . DIRECTORY_SEPARATOR . 'layout' . DIRECTORY_SEPARATOR . 'activity_main.xml',
250261
'<?xml version="1.0" encoding="utf-8"?>
251262
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
252263
xmlns:app="http://schemas.android.com/apk/res-auto"
@@ -268,16 +279,16 @@ private function createProject()
268279
0
269280
);
270281
}
271-
file_put_contents($this->projectName . '\\' . $moduleName . '\src\main\res\values\strings.xml', "<resources>\r\n\t" . '<string name="app_name">' . ucfirst($moduleName) . "</string>\r\n</resources>\r\n", 0);
282+
file_put_contents($this->projectName . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'main' . DIRECTORY_SEPARATOR . 'res' . DIRECTORY_SEPARATOR . 'values' . DIRECTORY_SEPARATOR . 'strings.xml', "<resources>\r\n\t" . '<string name="app_name">' . ucfirst($moduleName) . "</string>\r\n</resources>\r\n", 0);
272283

273284
$settingsContent .= "':" . $moduleName . "'";
274285

275286
if ($key < (count($this->modules) - 1)) {
276287
$settingsContent .= ",";
277288
}
278289
}
279-
file_put_contents($this->projectName . '\settings.gradle', $settingsContent . "\r\n", 0);
280-
// file_put_contents($this->projectName . '\local.properties', "ndk.dir=E\:\\SDK\\ndk-bundle\r\nsdk.dir=E\:\\SDK\r\n", 0);
290+
file_put_contents($this->projectName . DIRECTORY_SEPARATOR . 'settings.gradle', $settingsContent . "\r\n", 0);
291+
// file_put_contents($this->projectName . DIRECTORY_SEPARATOR . 'local.properties', "ndk.dir=E\:\\SDK\\ndk-bundle\r\nsdk.dir=E\:\\SDK\r\n", 0);
281292

282293
$this->output->writeln('<info>' . $this->projectName . ' created successfully!</info>');
283294
}
@@ -367,6 +378,7 @@ private function str_contains($haystack, $needles)
367378
private function _mkdir($dest)
368379
{
369380
if (!file_exists($dest) || !is_dir($dest)) {
381+
//$this->_print_info("$dest\n");
370382
mkdir($dest);
371383
}
372384
}

0 commit comments

Comments
 (0)