Skip to content

Commit

Permalink
updated to dcli 4.x and using pubspec_manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsutton committed Feb 18, 2024
1 parent b184325 commit 0d1fc5e
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 76 deletions.
7 changes: 3 additions & 4 deletions lib/src/commands/export.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand All @@ -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('''
Expand Down
6 changes: 3 additions & 3 deletions lib/src/commands/import.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/commands/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class OnePubLoginCommand extends Command<int> {
final settings = OnePubSettings.use()
..obfuscatedOrganisationId = auth.obfuscatedOrganisationId
..organisationName = auth.organisationName
..operatorEmail = auth.operatorEmail
..save();
..operatorEmail = auth.operatorEmail;
await settings.save();

final onepubApiUrl = settings.onepubApiUrlAsString;

Expand Down
54 changes: 25 additions & 29 deletions lib/src/commands/pub/private.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
* Written by Brett Sutton <[email protected]>, 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';

Expand Down Expand Up @@ -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')}
''');
Expand Down
2 changes: 1 addition & 1 deletion lib/src/entry_point.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Future<void> 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);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ class APIException extends OnePubCliException {
class NotInitialisedException extends OnePubCliException {
NotInitialisedException(super.message);
}

class SettingsException extends OnePubCliException {
SettingsException(super.message);
}
6 changes: 3 additions & 3 deletions lib/src/my_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -62,7 +62,7 @@ class MyRunner extends CommandRunner<int> implements PubTopLevel {
}
CommandSet commandSet;

void init() {
Future<void> init() async {
PubTopLevel.addColorFlag(argParser);

results = argParser.parse(args);
Expand All @@ -76,7 +76,7 @@ class MyRunner extends CommandRunner<int> implements PubTopLevel {
}

if (commandSet == CommandSet.onepub) {
OnePubSettings.install(dev: results['dev'] as bool);
await OnePubSettings.install(dev: results['dev'] as bool);
}
}

Expand Down
23 changes: 12 additions & 11 deletions lib/src/onepub_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ class OnePubSettings {
String get operatorEmail => _settings.asString('operatorEmail');

// ignore: discarded_futures
void save() => waitForEx(_settings.save());
Future<void> save() async => _settings.save();

void saveTo(String tempSettingsDir) {
Future<void> saveTo(String tempSettingsDir) async {
_settings.filePath = join(tempSettingsDir, defaultSettingsFilename);
// ignore: discarded_futures
waitForEx(_settings.save());
await _settings.save();
}

String resolveApiEndPoint(String command, {String? queryParams}) {
Expand Down Expand Up @@ -241,7 +241,7 @@ class OnePubSettings {
// });
// }

static void install({required bool dev}) {
static Future<void> install({required bool dev}) async {
final create = !exists(defaultPathToSettings);
final settings = OnePubSettings._load(create: create);

Expand All @@ -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.'));
}
Expand All @@ -268,23 +268,24 @@ class OnePubSettings {
}

///
void config({required bool dev}) {
promptForConfig(dev: dev);
Future<void> config({required bool dev}) async {
print('Configure OnePub');

await promptForConfig(dev: dev);
}

final testingFlagPath = join(HOME, '.onepubtesting');

void promptForConfig({required bool dev}) {
Future<void> promptForConfig({required bool dev}) async {
var url = OnePubSettings.defaultOnePubUrl;
if (dev) {
print('Configure OnePub');
url = ask('OnePub URL:', validator: UrlValidator(), defaultValue: url);
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)
Expand Down
3 changes: 1 addition & 2 deletions lib/src/util/token_export_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class TokenExportFile {
late final String pathToExportFile;
late final SettingsYaml settings;

// ignore: discarded_futures
void save() => waitForEx(settings.save());
Future<void> save() async => settings.save();

bool get hasToken => settings.validString(onepubTokenKey);
String get onepubToken => settings.asString(onepubTokenKey);
Expand Down
27 changes: 21 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/impersonate_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ Future<void> 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 {
Expand Down
19 changes: 12 additions & 7 deletions test/src/commands/pub/add_dep_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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')
Expand All @@ -62,16 +67,16 @@ 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 <dep>
final progress = 'dart $pathToOnePubScript pub add test_packag_2'
.start(workingDirectory: dartProject.pathToProjectRoot);
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);
});
});
});
Expand Down
7 changes: 4 additions & 3 deletions test/src/commands/pub/private_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;

Expand All @@ -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));
Expand Down
Loading

0 comments on commit 0d1fc5e

Please sign in to comment.