Skip to content
Merged
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-nd/4.0/80x15.png" /></a>


## Credits

Authors: Alexander Shvets ([@neochief](https://github.com/neochief)), ilopX ([@ilopX](https://github.com/ilopX))
173 changes: 173 additions & 0 deletions bin/deploy_flutter_demos.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
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<void> init() async {
print('Use temp: $tmpDir');
originUrl = await repositoryOriginUrl(projectDir);
}

Future<void> 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<void> cloneOriginRepository() async {
print('Cloning origin: [web-demos] $originUrl');
await cmd(
'git clone -b web-demos --single-branch $originUrl .',
workingDirectory: tmpDir,
);
}

Future<void> 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<void> 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<String> 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<String> 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<String> 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);
}