224 lines
6.1 KiB
Dart
224 lines
6.1 KiB
Dart
// ignore_for_file: constant_identifier_names, depend_on_referenced_packages
|
|
|
|
import 'dart:io';
|
|
import 'package:dart_framework/utils/path.dart';
|
|
import 'package:dart_framework/utils/system_util.dart';
|
|
import 'package:oto_cli/cli/cli.dart';
|
|
import 'package:oto_cli/oto/application.dart';
|
|
import 'package:oto_cli/oto/commands/build/app_data_creator.dart';
|
|
import 'package:oto_cli/oto/commands/build/build_dart.dart';
|
|
import 'package:oto_cli/oto/commands/build/build_dart_compile.dart';
|
|
import 'package:oto_cli/oto/commands/build/build_dot_net.dart';
|
|
import 'package:oto_cli/oto/commands/build/build_flutter.dart';
|
|
import 'package:oto_cli/oto/commands/build/build_ios.dart';
|
|
import 'package:oto_cli/oto/commands/git/git.dart';
|
|
import 'package:oto_cli/oto/commands/util/json.dart';
|
|
import 'package:oto_cli/oto/data/command_data.dart';
|
|
import 'package:oto_cli/oto/data/jenkins_data.dart';
|
|
import 'package:oto_cli/oto/commands/file/delete.dart';
|
|
import 'package:oto_cli/oto/commands/file/rename.dart';
|
|
import 'package:oto_cli/oto/commands/shell/shell.dart';
|
|
import 'package:oto_cli/oto/commands/file/copy.dart';
|
|
import 'package:oto_cli/oto/commands/util/execute_app.dart';
|
|
import 'package:oto_cli/oto/commands/util/publish_ios.dart';
|
|
import 'package:oto_cli/oto/commands/util/slack.dart';
|
|
import 'package:oto_cli/oto/commands/util/zip.dart';
|
|
import 'package:oto_cli/oto/commands/uploader/uploader.dart';
|
|
import 'package:path/path.dart' as pathlib;
|
|
|
|
enum CommandType {
|
|
SimpleCommand,
|
|
CreateAppData,
|
|
BuildDart,
|
|
BuildFlutter,
|
|
BuildDartCompile,
|
|
BuildDotNet,
|
|
BuildiOS,
|
|
ArchiveiOS,
|
|
ExportiOS,
|
|
PublishiOS,
|
|
ExecuteApp,
|
|
Upload,
|
|
Copy,
|
|
Rename,
|
|
Delete,
|
|
Slack,
|
|
SlackBuild,
|
|
Zip,
|
|
Shell,
|
|
Git,
|
|
GitRev,
|
|
GitCount,
|
|
JsonReader
|
|
}
|
|
|
|
abstract class Command {
|
|
static final Map<CommandType, Command> _commandMap = {
|
|
CommandType.SimpleCommand: SimpleCommand(),
|
|
CommandType.CreateAppData: CreateAppData(),
|
|
CommandType.BuildDart: BuildDart(),
|
|
CommandType.BuildFlutter: BuildFlutter(),
|
|
CommandType.BuildDartCompile: BuildDartCompile(),
|
|
CommandType.BuildDotNet: BuildDotNet(),
|
|
CommandType.BuildiOS: BuildiOS(),
|
|
CommandType.ArchiveiOS: ArchiveiOS(),
|
|
CommandType.ExportiOS: ExportiOS(),
|
|
CommandType.PublishiOS: PublishiOS(),
|
|
CommandType.ExecuteApp: ExecuteApp(),
|
|
CommandType.Upload: Uploader(),
|
|
CommandType.Copy: Copy(),
|
|
CommandType.Rename: Rename(),
|
|
CommandType.Delete: Delete(),
|
|
CommandType.Slack: Slack(),
|
|
CommandType.SlackBuild: SlackBuild(),
|
|
CommandType.Zip: Zip(),
|
|
CommandType.Shell: Shell(),
|
|
CommandType.Git: Git(),
|
|
CommandType.GitRev: GitRev(),
|
|
CommandType.GitCount: GitCount(),
|
|
CommandType.JsonReader: JsonReader()
|
|
};
|
|
|
|
factory Command.byType(CommandType type) {
|
|
return _commandMap[type]!;
|
|
}
|
|
|
|
Command();
|
|
|
|
static RegExp regEx = RegExp(r'(<!)([_a-zA-Z0-9.-]+)(>)');
|
|
|
|
static Future setReturnValue(String tag, dynamic value) async {
|
|
var match = regEx.firstMatch(tag);
|
|
tag = match!.group(2)!;
|
|
if (tag.startsWith('property')) {
|
|
var key = tag.replaceFirst('property.', '');
|
|
Application.instance.property[key] = value;
|
|
await CLI.println('[Return] $key : $value', color: Color.yellowStrong);
|
|
}
|
|
return simpleFuture;
|
|
}
|
|
|
|
static dynamic _getTagValue(String value) {
|
|
var setted = false;
|
|
dynamic data;
|
|
if (value.startsWith('property.')) {
|
|
var map = Application.instance.property;
|
|
var param = value.replaceFirst('property.', '');
|
|
if (map.containsKey(param)) {
|
|
data = map[param];
|
|
setted = true;
|
|
} else {
|
|
print('"$value" is not exist in property');
|
|
data = null;
|
|
setted = true;
|
|
}
|
|
}
|
|
|
|
if (value.startsWith('state.')) {
|
|
var map = Application.instance.commandStates;
|
|
var param = value.replaceFirst('state.', '');
|
|
if (map.containsKey(param)) {
|
|
var value = map[param].toString().replaceAll('CommandState.', '');
|
|
data = value == '' ? null : value;
|
|
} else {
|
|
data = null;
|
|
}
|
|
setted = true;
|
|
}
|
|
|
|
if (!setted) {
|
|
throw Exception(
|
|
'Command.getTagValue: "$value" is not defined in property.');
|
|
}
|
|
return data;
|
|
}
|
|
|
|
static String replaceTagValue(String raw) {
|
|
var list = regEx.allMatches(raw);
|
|
for (var match in list) {
|
|
var param = match.group(2);
|
|
var target = '<!${param!}>';
|
|
raw = raw.replaceAll(target, _getTagValue(param).toString());
|
|
}
|
|
return raw;
|
|
}
|
|
|
|
Map<String, dynamic> get property {
|
|
return Application.instance.property;
|
|
}
|
|
|
|
DataJenkins? get jenkinsData {
|
|
return Application.instance.jenkinsData;
|
|
}
|
|
|
|
String getWorkspace(String? dataWorkspace) {
|
|
return getAbs(dataWorkspace ?? jenkinsData!.workspace);
|
|
}
|
|
|
|
String getAbs(String path) {
|
|
path = getPathNormal(path);
|
|
if (path == '.' || path.startsWith('./')) {
|
|
path = path.replaceFirst('.', getPathNormal(pathlib.current));
|
|
}
|
|
return path;
|
|
}
|
|
|
|
String getCDPath(String path) {
|
|
String drive = '';
|
|
if (Platform.isWindows) {
|
|
drive = '${path[0]}: && ';
|
|
}
|
|
return '${drive}cd $path';
|
|
}
|
|
|
|
String getPathNormal(String path) {
|
|
return replaceTag(Path.getNormal(path));
|
|
}
|
|
|
|
String replaceTag(String raw) {
|
|
return Command.replaceTagValue(raw);
|
|
}
|
|
|
|
Future setReturn(String tag, dynamic value) async {
|
|
await Command.setReturnValue(tag, value);
|
|
}
|
|
|
|
Future execute(DataCommand command) async {
|
|
return simpleFuture;
|
|
}
|
|
|
|
Future complete(int exitCode, DataCommand command,
|
|
{String? errorMessage, List<int>? passCode}) {
|
|
passCode ??= [0];
|
|
if (passCode.contains(exitCode)) {
|
|
return simpleFuture;
|
|
} else {
|
|
print('Exit Code: $exitCode');
|
|
var data = ExceptionData();
|
|
data.phase = command.name;
|
|
data.message = errorMessage;
|
|
throw Exception(data);
|
|
}
|
|
}
|
|
}
|
|
|
|
//Templete
|
|
class SimpleCommand extends Command {
|
|
@override
|
|
Future execute(DataCommand command) async {
|
|
print(command.param);
|
|
return simpleFuture;
|
|
}
|
|
}
|
|
|
|
class ExceptionData {
|
|
String? phase;
|
|
String? message;
|
|
|
|
@override
|
|
String toString() {
|
|
var str = ' [$phase]\n\n';
|
|
str += message ??= '';
|
|
return str;
|
|
}
|
|
}
|