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