From 8e36aa31d5929474bc9392a18881614919b102b9 Mon Sep 17 00:00:00 2001 From: ilopX Date: Sat, 5 Feb 2022 11:01:39 +0200 Subject: [PATCH 1/5] Add deploy flutter demos script. --- bin/depoly_flutter_demos.dart | 169 ++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 bin/depoly_flutter_demos.dart diff --git a/bin/depoly_flutter_demos.dart b/bin/depoly_flutter_demos.dart new file mode 100644 index 0000000..3e91dd4 --- /dev/null +++ b/bin/depoly_flutter_demos.dart @@ -0,0 +1,169 @@ +import 'dart:io'; + +void main() async { + await init(); + + final prepareRepository = Future.microtask(() async { + await cloneOriginRepository(); + await fetchUpstream(); + }); + + final buildProject = Future.microtask(() async { + await buildWebProject(); + }); + + await Future.wait([prepareRepository, buildProject]); + + copyFiles(); + await pushToOrigin(); + clear(); +} + +Future init() async { + print('Use temp: $tmpDir'); + originUrl = await repositoryOriginUrl(projectDir); +} + +Future buildWebProject() async { + final flutterTargetFile = '${projectDir.path}bin\\main.dart'; + print('Build web app: $flutterTargetFile'); + await cmd('flutter build web -t $flutterTargetFile --web-renderer html'); +} + +Future cloneOriginRepository() async { + print('Cloning origin: [web-demos] $originUrl'); + await cmd( + 'git clone -b web-demos --single-branch $originUrl .', + workingDirectory: tmpDir, + ); +} + +Future fetchUpstream() async { + final shvets = r'https://github.com/RefactoringGuru/design-patterns-dart.git'; + print('Fetch upstream: [web-demos] $shvets'); + await cmd('git remote add upstream $shvets', workingDirectory: tmpDir); + await cmd('git fetch upstream', workingDirectory: tmpDir); +} + +void copyFiles() { + print('Copy files:'); + copyDirectory(webBuildDir, tmpDir); +} + +Future pushToOrigin() async { + await cmd('git add .', workingDirectory: tmpDir); + await cmd( + 'git commit -m ${await lastProjectCommit()}', + workingDirectory: tmpDir, + showOut: true, + ); + + print('Push to origin: [web-demos] $originUrl'); + await cmd('git push origin web-demos', workingDirectory: tmpDir, showOut: true); +} + +late final tmpDir = Directory.systemTemp.createTempSync(); +late final projectDir = thisPath(r'..\'); +late final webBuildDir = Directory(projectDir.uri.toFilePath() + r'build\web'); +late final String originUrl; + +void clear() { + print('Clear: $tmpDir'); + tmpDir.deleteSync(recursive: true); +} + +Future cmd( + String command, { + Directory? workingDirectory, + bool showOut = false, +}) async { + var process = await Process.run( + command, + [], + workingDirectory: workingDirectory?.path, + runInShell: true, + ); + + if (showOut) { + print(process.stdout); + } + + if (process.exitCode != 0) { + print(command); + print(process.stderr); + clear(); + exit(process.exitCode); + } + + return process.stdout; +} + +Future repositoryOriginUrl(Directory workingDir) async { + final raw = await cmd( + 'git remote show origin', + workingDirectory: workingDir, + ); + final url = RegExp('Push URL: (.+)\n').firstMatch(raw)?.group(1); + + if (url == null) { + throw Exception('Empty Remote repository'); + } + + return url; +} + +Future lastProjectCommit() async { + final rawCommit = + await cmd('git log -1 --pretty=%B', workingDirectory: projectDir); + final formatCommit = rawCommit.replaceAll(' ', '_'); + return 'auto_commit:_$formatCommit'; +} + +Directory thisPath(String name) { + final dir = Platform.script.pathSegments + .sublist(0, Platform.script.pathSegments.length - 1) + .join(Platform.pathSeparator) + + Platform.pathSeparator + + name; + + return Directory(Uri.directory(dir).normalizePath().toFilePath()); +} + +void copyDirectory(Directory source, Directory target) { + if (!target.existsSync()) { + target.createSync(); + } + + for (final entry in source.listSync()) { + final newPath = updatePath(entry, source, target); + + if (entry is File) { + copyFile(entry, newPath); + } else if (entry is Directory) { + copyDirectory(entry, Directory(newPath)); + } else { + throw Exception( + 'FileSystemEntity is not recognized. It must be a file or a directory', + ); + } + } +} + +void copyFile(File entry, String newFileName) { + print('\t${removerBuildPath(entry)}'); + entry.copySync(newFileName); +} + +String updatePath( + FileSystemEntity entry, + Directory source, + Directory target, +) { + return entry.path + .replaceFirst(source.path, target.path + Platform.pathSeparator) + .replaceAll(r'\\', r'\'); +} + +String removerBuildPath(FileSystemEntity target) { + return target.path.replaceFirst(webBuildDir.path, '').substring(1); +} From 832b6669fba81053f51c84aaae0d6c1acb4fbe07 Mon Sep 17 00:00:00 2001 From: ilopX Date: Sat, 5 Feb 2022 12:14:45 +0200 Subject: [PATCH 2/5] Add deploy-script description. --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3e1ca32..f4216c6 100644 --- a/README.md +++ b/README.md @@ -72,13 +72,19 @@ cd root directory flutter build web -t bin\main.dart --web-renderer html ``` -## License +### Deploy flutter demos +1. Fork this repo: https://github.com/RefactoringGuru/design-patterns-dart +2. Apply your changes. +3. Run the script `dart bin\deploy_flutter_demos.dart`. +This script will build a web platform flutter app and push the changes to your **web-demos** branch on github. +4. You can now make a pull request on the **web-demos** branch. +5. Once approved for the merge, the web app will be available at https://refactoringguru.github.io/design-patterns-dart . +## License This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. Creative Commons License ## Credits - Authors: Alexander Shvets ([@neochief](https://github.com/neochief)), ilopX ([@ilopX](https://github.com/ilopX)) From df97736638b3697add220967de4334a395061564 Mon Sep 17 00:00:00 2001 From: ilopX Date: Sat, 5 Feb 2022 12:20:19 +0200 Subject: [PATCH 3/5] Fix deploy-script name. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f4216c6..4f078e8 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ flutter build web -t bin\main.dart --web-renderer html ### Deploy flutter demos 1. Fork this repo: https://github.com/RefactoringGuru/design-patterns-dart 2. Apply your changes. -3. Run the script `dart bin\deploy_flutter_demos.dart`. +3. Run the script `dart bin\depoly_flutter_demos.dart`. This script will build a web platform flutter app and push the changes to your **web-demos** branch on github. 4. You can now make a pull request on the **web-demos** branch. 5. Once approved for the merge, the web app will be available at https://refactoringguru.github.io/design-patterns-dart . From 2fbd9875173e647d2fd6f6ceea340cbf6f8fae6f Mon Sep 17 00:00:00 2001 From: ilopX Date: Sat, 5 Feb 2022 12:23:15 +0200 Subject: [PATCH 4/5] Format deploy_flutter_demos.dart. --- bin/depoly_flutter_demos.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bin/depoly_flutter_demos.dart b/bin/depoly_flutter_demos.dart index 3e91dd4..b6e0509 100644 --- a/bin/depoly_flutter_demos.dart +++ b/bin/depoly_flutter_demos.dart @@ -59,7 +59,11 @@ Future pushToOrigin() async { ); print('Push to origin: [web-demos] $originUrl'); - await cmd('git push origin web-demos', workingDirectory: tmpDir, showOut: true); + await cmd( + 'git push origin web-demos', + workingDirectory: tmpDir, + showOut: true, + ); } late final tmpDir = Directory.systemTemp.createTempSync(); From 6542ba46fbbaf556ce3b4ccd0e963b29d17708bf Mon Sep 17 00:00:00 2001 From: ilopX Date: Sat, 5 Feb 2022 12:28:46 +0200 Subject: [PATCH 5/5] Fix deploy file name. --- README.md | 2 +- bin/{depoly_flutter_demos.dart => deploy_flutter_demos.dart} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename bin/{depoly_flutter_demos.dart => deploy_flutter_demos.dart} (100%) diff --git a/README.md b/README.md index 4f078e8..f4216c6 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ flutter build web -t bin\main.dart --web-renderer html ### Deploy flutter demos 1. Fork this repo: https://github.com/RefactoringGuru/design-patterns-dart 2. Apply your changes. -3. Run the script `dart bin\depoly_flutter_demos.dart`. +3. Run the script `dart bin\deploy_flutter_demos.dart`. This script will build a web platform flutter app and push the changes to your **web-demos** branch on github. 4. You can now make a pull request on the **web-demos** branch. 5. Once approved for the merge, the web app will be available at https://refactoringguru.github.io/design-patterns-dart . diff --git a/bin/depoly_flutter_demos.dart b/bin/deploy_flutter_demos.dart similarity index 100% rename from bin/depoly_flutter_demos.dart rename to bin/deploy_flutter_demos.dart