Skip to content

Commit 1843a38

Browse files
authored
Merge pull request #25 from ilopX/depoly-flutter-demos-script
Add an automated script to deploy Flutter demos.
2 parents a212519 + 6542ba4 commit 1843a38

File tree

2 files changed

+181
-2
lines changed

2 files changed

+181
-2
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,19 @@ cd root directory
7272
flutter build web -t bin\main.dart --web-renderer html
7373
```
7474

75-
## License
75+
### Deploy flutter demos
76+
1. Fork this repo: https://github.com/RefactoringGuru/design-patterns-dart
77+
2. Apply your changes.
78+
3. Run the script `dart bin\deploy_flutter_demos.dart`.
79+
This script will build a web platform flutter app and push the changes to your **web-demos** branch on github.
80+
4. You can now make a pull request on the **web-demos** branch.
81+
5. Once approved for the merge, the web app will be available at https://refactoringguru.github.io/design-patterns-dart .
7682

83+
## License
7784
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
7885

7986
<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>
8087

8188

8289
## Credits
83-
8490
Authors: Alexander Shvets ([@neochief](https://github.com/neochief)), ilopX ([@ilopX](https://github.com/ilopX))

bin/deploy_flutter_demos.dart

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import 'dart:io';
2+
3+
void main() async {
4+
await init();
5+
6+
final prepareRepository = Future.microtask(() async {
7+
await cloneOriginRepository();
8+
await fetchUpstream();
9+
});
10+
11+
final buildProject = Future.microtask(() async {
12+
await buildWebProject();
13+
});
14+
15+
await Future.wait([prepareRepository, buildProject]);
16+
17+
copyFiles();
18+
await pushToOrigin();
19+
clear();
20+
}
21+
22+
Future<void> init() async {
23+
print('Use temp: $tmpDir');
24+
originUrl = await repositoryOriginUrl(projectDir);
25+
}
26+
27+
Future<void> buildWebProject() async {
28+
final flutterTargetFile = '${projectDir.path}bin\\main.dart';
29+
print('Build web app: $flutterTargetFile');
30+
await cmd('flutter build web -t $flutterTargetFile --web-renderer html');
31+
}
32+
33+
Future<void> cloneOriginRepository() async {
34+
print('Cloning origin: [web-demos] $originUrl');
35+
await cmd(
36+
'git clone -b web-demos --single-branch $originUrl .',
37+
workingDirectory: tmpDir,
38+
);
39+
}
40+
41+
Future<void> fetchUpstream() async {
42+
final shvets = r'https://github.com/RefactoringGuru/design-patterns-dart.git';
43+
print('Fetch upstream: [web-demos] $shvets');
44+
await cmd('git remote add upstream $shvets', workingDirectory: tmpDir);
45+
await cmd('git fetch upstream', workingDirectory: tmpDir);
46+
}
47+
48+
void copyFiles() {
49+
print('Copy files:');
50+
copyDirectory(webBuildDir, tmpDir);
51+
}
52+
53+
Future<void> pushToOrigin() async {
54+
await cmd('git add .', workingDirectory: tmpDir);
55+
await cmd(
56+
'git commit -m ${await lastProjectCommit()}',
57+
workingDirectory: tmpDir,
58+
showOut: true,
59+
);
60+
61+
print('Push to origin: [web-demos] $originUrl');
62+
await cmd(
63+
'git push origin web-demos',
64+
workingDirectory: tmpDir,
65+
showOut: true,
66+
);
67+
}
68+
69+
late final tmpDir = Directory.systemTemp.createTempSync();
70+
late final projectDir = thisPath(r'..\');
71+
late final webBuildDir = Directory(projectDir.uri.toFilePath() + r'build\web');
72+
late final String originUrl;
73+
74+
void clear() {
75+
print('Clear: $tmpDir');
76+
tmpDir.deleteSync(recursive: true);
77+
}
78+
79+
Future<String> cmd(
80+
String command, {
81+
Directory? workingDirectory,
82+
bool showOut = false,
83+
}) async {
84+
var process = await Process.run(
85+
command,
86+
[],
87+
workingDirectory: workingDirectory?.path,
88+
runInShell: true,
89+
);
90+
91+
if (showOut) {
92+
print(process.stdout);
93+
}
94+
95+
if (process.exitCode != 0) {
96+
print(command);
97+
print(process.stderr);
98+
clear();
99+
exit(process.exitCode);
100+
}
101+
102+
return process.stdout;
103+
}
104+
105+
Future<String> repositoryOriginUrl(Directory workingDir) async {
106+
final raw = await cmd(
107+
'git remote show origin',
108+
workingDirectory: workingDir,
109+
);
110+
final url = RegExp('Push URL: (.+)\n').firstMatch(raw)?.group(1);
111+
112+
if (url == null) {
113+
throw Exception('Empty Remote repository');
114+
}
115+
116+
return url;
117+
}
118+
119+
Future<String> lastProjectCommit() async {
120+
final rawCommit =
121+
await cmd('git log -1 --pretty=%B', workingDirectory: projectDir);
122+
final formatCommit = rawCommit.replaceAll(' ', '_');
123+
return 'auto_commit:_$formatCommit';
124+
}
125+
126+
Directory thisPath(String name) {
127+
final dir = Platform.script.pathSegments
128+
.sublist(0, Platform.script.pathSegments.length - 1)
129+
.join(Platform.pathSeparator) +
130+
Platform.pathSeparator +
131+
name;
132+
133+
return Directory(Uri.directory(dir).normalizePath().toFilePath());
134+
}
135+
136+
void copyDirectory(Directory source, Directory target) {
137+
if (!target.existsSync()) {
138+
target.createSync();
139+
}
140+
141+
for (final entry in source.listSync()) {
142+
final newPath = updatePath(entry, source, target);
143+
144+
if (entry is File) {
145+
copyFile(entry, newPath);
146+
} else if (entry is Directory) {
147+
copyDirectory(entry, Directory(newPath));
148+
} else {
149+
throw Exception(
150+
'FileSystemEntity is not recognized. It must be a file or a directory',
151+
);
152+
}
153+
}
154+
}
155+
156+
void copyFile(File entry, String newFileName) {
157+
print('\t${removerBuildPath(entry)}');
158+
entry.copySync(newFileName);
159+
}
160+
161+
String updatePath(
162+
FileSystemEntity entry,
163+
Directory source,
164+
Directory target,
165+
) {
166+
return entry.path
167+
.replaceFirst(source.path, target.path + Platform.pathSeparator)
168+
.replaceAll(r'\\', r'\');
169+
}
170+
171+
String removerBuildPath(FileSystemEntity target) {
172+
return target.path.replaceFirst(webBuildDir.path, '').substring(1);
173+
}

0 commit comments

Comments
 (0)