diff --git a/lib/src/commands/export.dart b/lib/src/commands/export.dart index 29a90c6..77e6f7a 100644 --- a/lib/src/commands/export.dart +++ b/lib/src/commands/export.dart @@ -64,7 +64,7 @@ run: onepub login 'Found $user'); } await API().checkVersion(); - final response = waitForEx(API().exportMemberToken(user)); + final response = await API().exportMemberToken(user); if (response.success) { onepubToken = response.token!; @@ -86,9 +86,8 @@ run: onepub login exitCode: 1, message: 'You may only pass one argument to --file.'); } - final exportFile = TokenExportFile(pathToFile) - ..onepubToken = onepubToken - ..save(); + final exportFile = TokenExportFile(pathToFile)..onepubToken = onepubToken; + await exportFile.save(); print(''' diff --git a/lib/src/commands/import.dart b/lib/src/commands/import.dart index aea3da7..7c5a88d 100644 --- a/lib/src/commands/import.dart +++ b/lib/src/commands/import.dart @@ -88,11 +88,11 @@ Use `onepub export` to obtain the OnePub token. throw ExitException(exitCode: 1, message: organisation.errorMessage!); } - OnePubSettings.use() + final settings = OnePubSettings.use() ..operatorEmail = 'not set during import' ..obfuscatedOrganisationId = organisation.obfuscatedId - ..organisationName = organisation.name - ..save(); + ..organisationName = organisation.name; + await settings.save(); OnePubTokenStore().addToken( onepubApiUrl: OnePubSettings.use().onepubApiUrlAsString, diff --git a/lib/src/commands/login.dart b/lib/src/commands/login.dart index 857943a..3df09ee 100644 --- a/lib/src/commands/login.dart +++ b/lib/src/commands/login.dart @@ -49,8 +49,8 @@ class OnePubLoginCommand extends Command { final settings = OnePubSettings.use() ..obfuscatedOrganisationId = auth.obfuscatedOrganisationId ..organisationName = auth.organisationName - ..operatorEmail = auth.operatorEmail - ..save(); + ..operatorEmail = auth.operatorEmail; + await settings.save(); final onepubApiUrl = settings.onepubApiUrlAsString; diff --git a/lib/src/commands/pub/private.dart b/lib/src/commands/pub/private.dart index 7a08f42..7f0eaa0 100644 --- a/lib/src/commands/pub/private.dart +++ b/lib/src/commands/pub/private.dart @@ -3,11 +3,9 @@ * Written by Brett Sutton , Jan 2022 */ -import 'dart:io'; - import 'package:args/command_runner.dart'; -import 'package:dcli/dcli.dart' hide PubSpec; -import 'package:pubspec2/pubspec2.dart'; +import 'package:dcli/dcli.dart'; +import 'package:pubspec_manager/pubspec_manager.dart'; import 'package:scope/scope.dart'; import 'package:url_builder/url_builder.dart'; @@ -66,36 +64,34 @@ run: onepub login final currentOrganisationName = settings.organisationName; final url = settings.onepubApiUrlAsString; - final pubspec = await PubSpec.loadFile(project.pathToPubSpec); - if (pubspec.publishTo != null) { - if (pubspec.publishTo.toString() == url) { - print(orange('${pubspec.name} is already a private package.')); - return; - } - - await API().checkVersion(); - final organisation = await getOrganisation(obfuscatedOrganisationId); - if (organisation == null) { - print(orange('${pubspec.name} is already a private package ' - 'for another organisation')); - } else { - print(orange('${pubspec.name} is already a private package of ' - '${organisation.name}')); - } - if (!confirm('Do you want to change the organisation to ' - '$currentOrganisationName?')) { - print(red('Action cancelled')); - return; - } + final pubspec = PubSpec.loadFromPath(project.pathToPubSpec); + if (pubspec.publishTo.toString() == url) { + print(orange('${pubspec.name} is already a private package.')); + return; + } + + await API().checkVersion(); + final organisation = await getOrganisation(obfuscatedOrganisationId); + if (organisation == null) { + print(orange('${pubspec.name} is already a private package ' + 'for another organisation')); + } else { + print(orange('${pubspec.name} is already a private package of ' + '${organisation.name}')); + } + if (!confirm('Do you want to change the organisation to ' + '$currentOrganisationName?')) { + print(red('Action cancelled')); + return; } - final pubspecUpdated = pubspec.copy(publishTo: Uri.parse(url)); - await pubspecUpdated.save(Directory(project.pathToProjectRoot)); + pubspec.publishTo.set(url); + pubspec.saveTo(project.pathToProjectRoot); print(''' -${pubspecUpdated.name} has been marked as a private package for the organisation ${settings.organisationName}. +${pubspec.name} has been marked as a private package for the organisation ${settings.organisationName}. -Run 'dart/flutter pub publish' to publish ${pubspecUpdated.name} to OnePub +Run 'dart/flutter pub publish' to publish ${pubspec.name} to OnePub See ${urlJoin(settings.onepubWebUrl, 'publish')} '''); diff --git a/lib/src/entry_point.dart b/lib/src/entry_point.dart index d1adf40..04ccb20 100644 --- a/lib/src/entry_point.dart +++ b/lib/src/entry_point.dart @@ -34,7 +34,7 @@ Future entrypoint( final runner = MyRunner(args, executableName, _description, commandSet); try { printPreamble(); - runner.init(); + await runner.init(); await runner.run(args); } on FormatException catch (e) { printerr(e.message); diff --git a/lib/src/exceptions.dart b/lib/src/exceptions.dart index e8555e5..72fddee 100644 --- a/lib/src/exceptions.dart +++ b/lib/src/exceptions.dart @@ -33,3 +33,7 @@ class APIException extends OnePubCliException { class NotInitialisedException extends OnePubCliException { NotInitialisedException(super.message); } + +class SettingsException extends OnePubCliException { + SettingsException(super.message); +} diff --git a/lib/src/my_runner.dart b/lib/src/my_runner.dart index d9bc441..ae5e4b8 100644 --- a/lib/src/my_runner.dart +++ b/lib/src/my_runner.dart @@ -37,8 +37,8 @@ import 'pub/command/upgrade.dart'; import 'pub/command/uploader.dart'; import 'pub/command/version.dart'; import 'pub/io.dart'; -import 'pub/log.dart' hide red; import 'pub/log.dart' as plog; +import 'pub/log.dart' hide red; enum CommandSet { opub, onepub } @@ -62,7 +62,7 @@ class MyRunner extends CommandRunner implements PubTopLevel { } CommandSet commandSet; - void init() { + Future init() async { PubTopLevel.addColorFlag(argParser); results = argParser.parse(args); @@ -76,7 +76,7 @@ class MyRunner extends CommandRunner implements PubTopLevel { } if (commandSet == CommandSet.onepub) { - OnePubSettings.install(dev: results['dev'] as bool); + await OnePubSettings.install(dev: results['dev'] as bool); } } diff --git a/lib/src/onepub_settings.dart b/lib/src/onepub_settings.dart index a5168c2..035a9a0 100644 --- a/lib/src/onepub_settings.dart +++ b/lib/src/onepub_settings.dart @@ -200,12 +200,12 @@ class OnePubSettings { String get operatorEmail => _settings.asString('operatorEmail'); // ignore: discarded_futures - void save() => waitForEx(_settings.save()); + Future save() async => _settings.save(); - void saveTo(String tempSettingsDir) { + Future saveTo(String tempSettingsDir) async { _settings.filePath = join(tempSettingsDir, defaultSettingsFilename); // ignore: discarded_futures - waitForEx(_settings.save()); + await _settings.save(); } String resolveApiEndPoint(String command, {String? queryParams}) { @@ -241,7 +241,7 @@ class OnePubSettings { // }); // } - static void install({required bool dev}) { + static Future install({required bool dev}) async { final create = !exists(defaultPathToSettings); final settings = OnePubSettings._load(create: create); @@ -251,7 +251,7 @@ class OnePubSettings { } if (settings.onepubUrl == null || settings.onepubUrl!.isEmpty || dev) { - settings.config(dev: dev); + await settings.config(dev: dev); print(orange('Installed OnePub version: $packageVersion.')); } @@ -268,13 +268,15 @@ class OnePubSettings { } /// - void config({required bool dev}) { - promptForConfig(dev: dev); + Future config({required bool dev}) async { + print('Configure OnePub'); + + await promptForConfig(dev: dev); } final testingFlagPath = join(HOME, '.onepubtesting'); - void promptForConfig({required bool dev}) { + Future promptForConfig({required bool dev}) async { var url = OnePubSettings.defaultOnePubUrl; if (dev) { print('Configure OnePub'); @@ -282,9 +284,8 @@ class OnePubSettings { testingFlagPath.write('onepubtesting'); } - OnePubSettings.use() - ..onepubUrl = url - ..save(); + final settings = OnePubSettings.use()..onepubUrl = url; + await settings.save(); } /// If the settings are using a non-standard url (e.g. not https;//onepub.dev) diff --git a/lib/src/util/token_export_file.dart b/lib/src/util/token_export_file.dart index 016f4af..2ca0c2e 100644 --- a/lib/src/util/token_export_file.dart +++ b/lib/src/util/token_export_file.dart @@ -32,8 +32,7 @@ class TokenExportFile { late final String pathToExportFile; late final SettingsYaml settings; - // ignore: discarded_futures - void save() => waitForEx(settings.save()); + Future save() async => settings.save(); bool get hasToken => settings.validString(onepubTokenKey); String get onepubToken => settings.asString(onepubTokenKey); diff --git a/pubspec.lock b/pubspec.lock index 422b3e1..08d4b5b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -189,10 +189,10 @@ packages: dependency: "direct main" description: name: file - sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d" + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" url: "https://pub.dev" source: hosted - version: "6.1.4" + version: "7.0.0" file_utils: dependency: "direct main" description: @@ -201,6 +201,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.1" + fixnum: + dependency: transitive + description: + name: fixnum + sha256: "25517a4deb0c03aa0f32fd12db525856438902d9c16536311e76cdc57b31d7d1" + url: "https://pub.dev" + source: hosted + version: "1.1.0" frontend_server_client: dependency: "direct main" description: @@ -428,8 +436,15 @@ packages: pubspec2: dependency: "direct main" description: - name: pubspec2 - sha256: "7b1fd81927f1da6d88457c83b51134e1bc8cb07638bd8d9e205b2ce1cd9ec091" + path: "../../pubspec_manager" + relative: true + source: path + version: "1.0.0-alpha.14" + quiver: + dependency: transitive + description: + name: quiver + sha256: b1c1ac5ce6688d77f65f3375a9abb9319b3cb32486bdc7a1e0fdf004d7ba4e47 url: "https://pub.dev" source: hosted version: "2.4.2" @@ -461,10 +476,10 @@ packages: dependency: "direct main" description: name: scope - sha256: e0c880d8f0db2ffd2accd63eeb02396748f3b8a2f71bce4b7d3f8dab75fc8a74 + sha256: "80cf1cb727791fdaaa4131817974a6084815ed59b9ab02ef352c3a1badea488b" url: "https://pub.dev" source: hosted - version: "3.0.0" + version: "4.1.0" settings_yaml: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index da13952..52bbaf5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -57,8 +57,8 @@ dependencies: pool: 1.5.1 posix: 5.0.0 pub_semver: 2.1.4 - pubspec2: 2.4.2 pubspec_lock: ^3.0.2 + pubspec_manager: ^1.0.0-alpha.14 quiver: 3.2.1 random_string: 2.3.1 scope: 3.0.0 diff --git a/test/impersonate_user.dart b/test/impersonate_user.dart index f40a390..f5b801e 100644 --- a/test/impersonate_user.dart +++ b/test/impersonate_user.dart @@ -60,8 +60,8 @@ Future impersonateMember({ ..operatorEmail = member.email ..onepubUrl = testSettings.onepubUrl ..organisationName = member.organisationName - ..obfuscatedOrganisationId = member.obfuscatedOrganisationId - ..save(); + ..obfuscatedOrganisationId = member.obfuscatedOrganisationId; + await settings.save(); // set an alternate location for the token store await OnePubTokenStore.withPathTo(tempSettingsDir, () async { diff --git a/test/src/commands/pub/add_dep_test.dart b/test/src/commands/pub/add_dep_test.dart index d5ecb37..3ec748f 100644 --- a/test/src/commands/pub/add_dep_test.dart +++ b/test/src/commands/pub/add_dep_test.dart @@ -8,7 +8,8 @@ import 'package:onepub/src/api/api.dart'; import 'package:onepub/src/entry_point.dart'; import 'package:onepub/src/my_runner.dart'; import 'package:path/path.dart' hide equals; -import 'package:pub_semver/pub_semver.dart'; +import 'package:pub_semver/pub_semver.dart' as ps; +import 'package:pubspec_manager/pubspec_manager.dart'; import 'package:scope/scope.dart'; import 'package:test/test.dart'; @@ -32,12 +33,16 @@ void main() { // increment the package 2 version number so we can publish it. final pathToPackage2Pubspec = dartProject.pathToPubSpec; - final pubspec = PubSpec.fromFile(pathToPackage2Pubspec); + final pubspec = PubSpec.loadFromPath(pathToPackage2Pubspec); + final versions = await API() .fetchVersions(member.obfuscatedOrganisationId, packageName); + final next = ps.Version.parse(versions.latest.version).nextMinor; + pubspec - ..version = Version.parse(versions.latest.version).nextMinor - ..save(pathToPackage2Pubspec); + ..version = VersionBuilder.parse(next.canonicalizedVersion) + .attach(pubspec) + ..saveTo(pathToPackage2Pubspec); // add new version to change log to stop pub publish complaining. join(pathToProjectRoot, 'CHANGELOG.md') @@ -62,7 +67,7 @@ void main() { member: member, action: () async { var pubSpec = dartProject.pubSpec; - expect(pubSpec.dependencies.containsKey('test_packag_2'), isFalse); + expect(pubSpec.dependencies.exists('test_packag_2'), isFalse); // run onepub add final progress = 'dart $pathToOnePubScript pub add test_packag_2' @@ -70,8 +75,8 @@ void main() { expect(progress.exitCode, equals(0)); // load the updated pubspec - pubSpec = PubSpec.fromFile(dartProject.pathToPubSpec); - expect(pubSpec.dependencies.containsKey('test_packag_2'), isTrue); + pubSpec = PubSpec.loadFromPath(dartProject.pathToPubSpec); + expect(pubSpec.dependencies.exists('test_packag_2'), isTrue); }); }); }); diff --git a/test/src/commands/pub/private_test.dart b/test/src/commands/pub/private_test.dart index 252c7d2..34614ca 100644 --- a/test/src/commands/pub/private_test.dart +++ b/test/src/commands/pub/private_test.dart @@ -8,6 +8,7 @@ import 'package:onepub/src/entry_point.dart'; import 'package:onepub/src/my_runner.dart'; import 'package:onepub/src/onepub_settings.dart'; import 'package:onepub/src/version/version.g.dart'; +import 'package:pubspec_manager/pubspec_manager.dart'; import 'package:scope/scope.dart'; import 'package:test/test.dart'; import 'package:url_builder/url_builder.dart'; @@ -31,7 +32,7 @@ void main() { action: () async { final settings = OnePubSettings.use(); final organisationName = settings.organisationName; - expect(dartProject.pubSpec.pubspec.publishTo, isNull); + expect(dartProject.pubSpec.publishTo, isNull); final size = stat(dartProject.pathToPubSpec).size; @@ -54,8 +55,8 @@ void main() { final url = OnePubSettings.use().onepubWebUrl; expect(clean.contains('See ${urlJoin(url, 'publish')}'), isTrue); - final pubSpec = PubSpec.fromFile(dartProject.pathToPubSpec); - expect(pubSpec.pubspec.publishTo.toString(), + final pubSpec = PubSpec.loadFromPath(dartProject.pathToPubSpec); + expect(pubSpec.publishTo.toString(), equals(settings.onepubApiUrlAsString)); expect(stat(dartProject.pathToPubSpec).size, greaterThan(size)); diff --git a/test/test_settings.dart b/test/test_settings.dart index 068a680..16ca98e 100644 --- a/test/test_settings.dart +++ b/test/test_settings.dart @@ -27,8 +27,7 @@ class TestSettings { // set organisationId(String obsfucatedId) => // _settings['organisationId'] = obsfucatedId; - // ignore: discarded_futures - void save() => waitForEx(_settings.save()); + Future save() async => _settings.save(); String get pathToTestSettings { final pathToTest = DartProject.self.pathToTestDir;