From d0484e6a8cffd4cfceb6df04188605fb203d746d Mon Sep 17 00:00:00 2001 From: Jens Horstmann Date: Thu, 3 Mar 2022 15:27:55 +0100 Subject: [PATCH 1/2] created cli --- .vscode/launch.json | 16 ++++++++++++++- packages/cli/.gitignore | 6 ++++++ packages/cli/CHANGELOG.md | 3 +++ packages/cli/README.md | 1 + packages/cli/analysis_options.yaml | 30 +++++++++++++++++++++++++++ packages/cli/bin/cli.dart | 27 ++++++++++++++++++++++++ packages/cli/pubspec.lock | 33 ++++++++++++++++++++++++++++++ packages/cli/pubspec.yaml | 17 +++++++++++++++ 8 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 packages/cli/.gitignore create mode 100644 packages/cli/CHANGELOG.md create mode 100644 packages/cli/README.md create mode 100644 packages/cli/analysis_options.yaml create mode 100644 packages/cli/bin/cli.dart create mode 100644 packages/cli/pubspec.lock create mode 100644 packages/cli/pubspec.yaml diff --git a/.vscode/launch.json b/.vscode/launch.json index a299873..d871a01 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -15,7 +15,8 @@ "--api_key", "zYzp8bqsbjZaKPIEVMmXaAxlki6rAe0xPbz5qvMiINdzwrmndu1Qe2lfFlQ9PWdw7c5986lTqJXVOk1MjIJJ6Q==" ] - },{ + }, + { "name": "run (gitlab)", "request": "launch", "type": "dart", @@ -27,6 +28,19 @@ "--api_key", "zYzp8bqsbjZaKPIEVMmXaAxlki6rAe0xPbz5qvMiINdzwrmndu1Qe2lfFlQ9PWdw7c5986lTqJXVOk1MjIJJ6Q==" ] + }, + { + "name": "debug (cli)", + "request": "launch", + "type": "dart", + "program": "bin/cli.dart", + "cwd": "packages/cli", + "args": [ + "--path", + "test_app/build/web", + "--api-key", + "1234test" + ] } ] diff --git a/packages/cli/.gitignore b/packages/cli/.gitignore new file mode 100644 index 0000000..3c8a157 --- /dev/null +++ b/packages/cli/.gitignore @@ -0,0 +1,6 @@ +# Files and directories created by pub. +.dart_tool/ +.packages + +# Conventional directory for build output. +build/ diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md new file mode 100644 index 0000000..effe43c --- /dev/null +++ b/packages/cli/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/packages/cli/README.md b/packages/cli/README.md new file mode 100644 index 0000000..a307539 --- /dev/null +++ b/packages/cli/README.md @@ -0,0 +1 @@ +A simple command-line application. diff --git a/packages/cli/analysis_options.yaml b/packages/cli/analysis_options.yaml new file mode 100644 index 0000000..dee8927 --- /dev/null +++ b/packages/cli/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/packages/cli/bin/cli.dart b/packages/cli/bin/cli.dart new file mode 100644 index 0000000..4a7bcb3 --- /dev/null +++ b/packages/cli/bin/cli.dart @@ -0,0 +1,27 @@ +import 'package:args/args.dart'; +import 'package:git/git.dart'; +import 'package:path/path.dart' as p; + +void main(List arguments) async { + final parser = ArgParser() + ..addOption('path', mandatory: true) + ..addOption('api-key', mandatory: true); + + final args = parser.parse(arguments); + + final path = args['path'] as String; + final apiKey = args['api-key'] as String; + + if (await GitDir.isGitDir(p.current)) { + final gitDir = await GitDir.fromExisting( + p.current, + allowSubdirectory: true, + ); + final branch = await gitDir.currentBranch(); + final branchName = branch.branchName; + final sha = branch.sha; + final int i = 9; + } else { + print('Please make sure to execute this CLI within a git folder.'); + } +} diff --git a/packages/cli/pubspec.lock b/packages/cli/pubspec.lock new file mode 100644 index 0000000..456be13 --- /dev/null +++ b/packages/cli/pubspec.lock @@ -0,0 +1,33 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + args: + dependency: "direct main" + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.0" + git: + dependency: "direct main" + description: + name: git + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + lints: + dependency: "direct dev" + description: + name: lints + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" + path: + dependency: transitive + description: + name: path + url: "https://pub.dartlang.org" + source: hosted + version: "1.8.1" +sdks: + dart: ">=2.15.0 <3.0.0" diff --git a/packages/cli/pubspec.yaml b/packages/cli/pubspec.yaml new file mode 100644 index 0000000..a049e17 --- /dev/null +++ b/packages/cli/pubspec.yaml @@ -0,0 +1,17 @@ +name: cli +description: A simple command-line application. +version: 1.0.0 +# homepage: https://www.example.com + +environment: + sdk: '>=2.15.0 <3.0.0' + + +# dependencies: +# path: ^1.8.0 + +dev_dependencies: + lints: ^1.0.0 +dependencies: + args: ^2.3.0 + git: ^2.0.0 From c2cfef076e9cd90d2f6a854e1b1a6666d5d7bf75 Mon Sep 17 00:00:00 2001 From: Jens Horstmann Date: Sun, 6 Mar 2022 14:54:24 +0100 Subject: [PATCH 2/2] matched implementation to the new backend --- .gitignore | 5 +- .vscode/launch.json | 6 +- .../bin/bitbucket_deployment_parser.dart | 5 +- packages/bitbucket/bin/main.dart | 13 +-- packages/cli/bin/cli.dart | 28 ++++++- packages/cli/pubspec.lock | 84 +++++++++++++++++++ packages/cli/pubspec.yaml | 10 +-- .../github/bin/github_deployment_parser.dart | 1 + packages/github/bin/main.dart | 6 +- .../gitlab/bin/gitlab_deployment_parser.dart | 5 +- packages/gitlab/bin/main.dart | 13 +-- .../lib/src/widgetbook_http_client.dart | 13 +-- .../lib/src/widgetbook_zip_encoder.dart | 3 +- .../lib/src/models/deployment_data.dart | 23 +++++ 14 files changed, 166 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 56fa1a6..ca34abf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ # Conventional directory for build output. build/ -.DS_Store \ No newline at end of file +.DS_Store + +assets/ +web.zip \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index d871a01..d880a0e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -36,10 +36,10 @@ "program": "bin/cli.dart", "cwd": "packages/cli", "args": [ - "--path", - "test_app/build/web", + "--path", + "../../assets/web", "--api-key", - "1234test" + "MKaFf0zMJYwWzSV2HclF8G2gVAkHmvTw" ] } diff --git a/packages/bitbucket/bin/bitbucket_deployment_parser.dart b/packages/bitbucket/bin/bitbucket_deployment_parser.dart index ff57e85..d67328e 100644 --- a/packages/bitbucket/bin/bitbucket_deployment_parser.dart +++ b/packages/bitbucket/bin/bitbucket_deployment_parser.dart @@ -9,11 +9,13 @@ class BitbucketDeploymentParser extends DeploymentParser { final branchName = map['BITBUCKET_BRANCH']; final repositoryName = map['BITBUCKET_REPO_FULL_NAME']; final commitSha = map['BITBUCKET_COMMIT']; + final apikey = map['WIDGETBOOK_API_KEY']; if (actor == null || branchName == null || repositoryName == null || - commitSha == null) { + commitSha == null || + apikey == null) { throw Exception('Not able to find expected information'); } @@ -23,6 +25,7 @@ class BitbucketDeploymentParser extends DeploymentParser { commitSha: commitSha, repositoryName: repositoryName, provider: 'BitBucket', + apiKey: apikey, ); } } diff --git a/packages/bitbucket/bin/main.dart b/packages/bitbucket/bin/main.dart index 51aaf71..ded6883 100644 --- a/packages/bitbucket/bin/main.dart +++ b/packages/bitbucket/bin/main.dart @@ -6,13 +6,12 @@ import 'bitbucket_deployment_parser.dart'; Future main(List arguments) async { final deploymentData = BitbucketDeploymentParser().parse(); - final apiKey = parseApiKey(); final buildPath = parseBuildPath(); final directory = Directory(buildPath); final file = WidgetbookZipEncoder().encode(directory); if (file != null) { - await WidgetbookHttpClient(apiKey: apiKey).uploadDeployment( + await WidgetbookHttpClient().uploadDeployment( deploymentFile: file, data: deploymentData, ); @@ -21,16 +20,6 @@ Future main(List arguments) async { } } -String parseApiKey() { - final apikey = Platform.environment['WIDGETBOOK_API_KEY']; - - if (apikey == null) { - throw Exception('Could not parse API key'); - } - - return apikey; -} - String parseBuildPath() { final buildPath = Platform.environment['WIDGETBOOK_BUILD_PATH']; diff --git a/packages/cli/bin/cli.dart b/packages/cli/bin/cli.dart index 4a7bcb3..b73745b 100644 --- a/packages/cli/bin/cli.dart +++ b/packages/cli/bin/cli.dart @@ -1,5 +1,9 @@ +import 'dart:io'; + import 'package:args/args.dart'; import 'package:git/git.dart'; +import 'package:helper/helper.dart'; +import 'package:parser/parser.dart'; import 'package:path/path.dart' as p; void main(List arguments) async { @@ -18,9 +22,31 @@ void main(List arguments) async { allowSubdirectory: true, ); final branch = await gitDir.currentBranch(); + + // final status = await gitDir.runCommand(['status']); + // final asString = status.stdout as String; + final branchName = branch.branchName; final sha = branch.sha; - final int i = 9; + + final directory = Directory(path); + + final file = WidgetbookZipEncoder().encode(directory); + if (file != null) { + await WidgetbookHttpClient().uploadDeployment( + deploymentFile: file, + data: DeploymentData( + branchName: branchName, + repositoryName: '', + commitSha: sha, + actor: '', + apiKey: apiKey, + provider: 'CLI', + ), + ); + } else { + print('Could not create .zip file for upload.'); + } } else { print('Please make sure to execute this CLI within a git folder.'); } diff --git a/packages/cli/pubspec.lock b/packages/cli/pubspec.lock index 456be13..7fa72b7 100644 --- a/packages/cli/pubspec.lock +++ b/packages/cli/pubspec.lock @@ -1,6 +1,13 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + archive: + dependency: transitive + description: + name: archive + url: "https://pub.dartlang.org" + source: hosted + version: "3.2.1" args: dependency: "direct main" description: @@ -8,6 +15,34 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.3.0" + charcode: + dependency: transitive + description: + name: charcode + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" + collection: + dependency: transitive + description: + name: collection + url: "https://pub.dartlang.org" + source: hosted + version: "1.15.0" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" + dio: + dependency: transitive + description: + name: dio + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.4" git: dependency: "direct main" description: @@ -15,6 +50,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.0" + helper: + dependency: "direct main" + description: + path: "../helper" + relative: true + source: path + version: "0.0.0" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.0" lints: dependency: "direct dev" description: @@ -22,6 +71,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.1" + parser: + dependency: "direct main" + description: + path: "../parser" + relative: true + source: path + version: "1.0.0" path: dependency: transitive description: @@ -29,5 +85,33 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.1" + source_span: + dependency: transitive + description: + name: source_span + url: "https://pub.dartlang.org" + source: hosted + version: "1.8.2" + string_scanner: + dependency: transitive + description: + name: string_scanner + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + term_glyph: + dependency: transitive + description: + name: term_glyph + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + typed_data: + dependency: transitive + description: + name: typed_data + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" sdks: dart: ">=2.15.0 <3.0.0" diff --git a/packages/cli/pubspec.yaml b/packages/cli/pubspec.yaml index a049e17..43db9d8 100644 --- a/packages/cli/pubspec.yaml +++ b/packages/cli/pubspec.yaml @@ -1,17 +1,15 @@ name: cli description: A simple command-line application. -version: 1.0.0 -# homepage: https://www.example.com environment: sdk: '>=2.15.0 <3.0.0' - -# dependencies: -# path: ^1.8.0 - dev_dependencies: lints: ^1.0.0 dependencies: args: ^2.3.0 git: ^2.0.0 + helper: + path: ../helper + parser: + path: ../parser diff --git a/packages/github/bin/github_deployment_parser.dart b/packages/github/bin/github_deployment_parser.dart index 8eee50d..de2e1c2 100644 --- a/packages/github/bin/github_deployment_parser.dart +++ b/packages/github/bin/github_deployment_parser.dart @@ -24,6 +24,7 @@ class GithubDeploymentParser extends DeploymentParser { commitSha: commitSha, repositoryName: repositoryName, provider: 'GitHub', + apiKey: '', ); } } diff --git a/packages/github/bin/main.dart b/packages/github/bin/main.dart index d7b729e..5483840 100644 --- a/packages/github/bin/main.dart +++ b/packages/github/bin/main.dart @@ -22,11 +22,13 @@ Future main(List arguments) async { final apiKey = parsedArguments['api_key'] as String; final directory = Directory(path); - final deploymentData = GithubDeploymentParser().parse(); + final deploymentData = GithubDeploymentParser().parse().copyWith( + apiKey: apiKey, + ); final file = WidgetbookZipEncoder().encode(directory); if (file != null) { - await WidgetbookHttpClient(apiKey: apiKey).uploadDeployment( + await WidgetbookHttpClient().uploadDeployment( deploymentFile: file, data: deploymentData, ); diff --git a/packages/gitlab/bin/gitlab_deployment_parser.dart b/packages/gitlab/bin/gitlab_deployment_parser.dart index 51c8fad..ae33233 100644 --- a/packages/gitlab/bin/gitlab_deployment_parser.dart +++ b/packages/gitlab/bin/gitlab_deployment_parser.dart @@ -7,11 +7,13 @@ class GitlabDeploymentParser extends DeploymentParser { final branchName = map['CI_COMMIT_BRANCH']; final repositoryName = map['CI_PROJECT_PATH']; final commitSha = map['CI_COMMIT_SHA']; + final apikey = map['WIDGETBOOK_API_KEY']; if (actor == null || branchName == null || repositoryName == null || - commitSha == null) { + commitSha == null || + apikey == null) { throw Exception('Not able to find expected information'); } @@ -21,6 +23,7 @@ class GitlabDeploymentParser extends DeploymentParser { commitSha: commitSha, repositoryName: repositoryName, provider: 'GitLab', + apiKey: apikey, ); } } diff --git a/packages/gitlab/bin/main.dart b/packages/gitlab/bin/main.dart index f5f1f23..16e3567 100644 --- a/packages/gitlab/bin/main.dart +++ b/packages/gitlab/bin/main.dart @@ -6,14 +6,13 @@ import 'gitlab_deployment_parser.dart'; Future main(List arguments) async { final deploymentData = GitlabDeploymentParser().parse(); - final apiKey = parseApiKey(); final buildPath = parseBuildPath(); final directory = Directory(buildPath); final file = WidgetbookZipEncoder().encode(directory); if (file != null) { - await WidgetbookHttpClient(apiKey: apiKey).uploadDeployment( + await WidgetbookHttpClient().uploadDeployment( deploymentFile: file, data: deploymentData, ); @@ -22,16 +21,6 @@ Future main(List arguments) async { } } -String parseApiKey() { - final apikey = Platform.environment['WIDGETBOOK_API_KEY']; - - if (apikey == null) { - throw Exception('Could not parse API key'); - } - - return apikey; -} - String parseBuildPath() { final buildPath = Platform.environment['WIDGETBOOK_BUILD_PATH']; diff --git a/packages/helper/lib/src/widgetbook_http_client.dart b/packages/helper/lib/src/widgetbook_http_client.dart index b4d992d..d93f0de 100644 --- a/packages/helper/lib/src/widgetbook_http_client.dart +++ b/packages/helper/lib/src/widgetbook_http_client.dart @@ -11,16 +11,10 @@ class WidgetbookHttpClient { /// Creates a new instance of [WidgetbookHttpClient]. WidgetbookHttpClient({ Dio? client, - required String apiKey, }) : client = client ?? Dio() { this.client.options.baseUrl = const bool.fromEnvironment('dart.vm.product') ? 'https://api.widgetbook.io/v1/' - : 'http://localhost:8080/v1/'; - - this.client.options.headers.putIfAbsent( - 'Authorization', - () => 'Bearer $apiKey', - ); + : 'http://localhost:3000/v1/'; } /// underlying [Dio] client @@ -33,7 +27,7 @@ class WidgetbookHttpClient { }) async { try { await client.post( - '/hosting', + '/builds/deploy', data: FormData.fromMap( { 'file': await MultipartFile.fromFile( @@ -45,7 +39,8 @@ class WidgetbookHttpClient { 'repository': data.repositoryName, 'actor': data.actor, 'commit': data.commitSha, - 'repository-provider': data.provider, + 'version-control-provider': data.provider, + 'api-key': data.apiKey, }, ), ); diff --git a/packages/helper/lib/src/widgetbook_zip_encoder.dart b/packages/helper/lib/src/widgetbook_zip_encoder.dart index cd0ba70..c3ce255 100644 --- a/packages/helper/lib/src/widgetbook_zip_encoder.dart +++ b/packages/helper/lib/src/widgetbook_zip_encoder.dart @@ -7,11 +7,12 @@ class WidgetbookZipEncoder { /// Encodes the directory to a .zip file File? encode(Directory directory) { if (directory.existsSync()) { - const fileName = 'deployment.zip'; + const fileName = 'web.zip'; ZipFileEncoder() ..create(fileName) ..addDirectory( directory, + includeDirName: false, ) ..close(); final createdZip = File(fileName); diff --git a/packages/parser/lib/src/models/deployment_data.dart b/packages/parser/lib/src/models/deployment_data.dart index 9b6acf5..3ff1b3d 100644 --- a/packages/parser/lib/src/models/deployment_data.dart +++ b/packages/parser/lib/src/models/deployment_data.dart @@ -7,8 +7,12 @@ class DeploymentData { required this.commitSha, required this.actor, required this.provider, + required this.apiKey, }); + /// The API key of a project + final String apiKey; + /// Name of the branch final String branchName; @@ -23,4 +27,23 @@ class DeploymentData { /// Git cloud provider e.g. GitHub, GitLab, etc final String provider; + + /// Copies the data with the provided fields + DeploymentData copyWith({ + String? apiKey, + String? branchName, + String? repositoryName, + String? commitSha, + String? actor, + String? provider, + }) { + return DeploymentData( + apiKey: apiKey ?? this.apiKey, + branchName: branchName ?? this.branchName, + repositoryName: repositoryName ?? this.repositoryName, + commitSha: commitSha ?? this.commitSha, + actor: actor ?? this.actor, + provider: provider ?? this.provider, + ); + } }