-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Init command #780
base: main
Are you sure you want to change the base?
feat: Init command #780
Changes from all commits
7a75010
9e673a4
c4a20ae
854a086
9a0e6f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,71 @@ | ||||||
import 'dart:io'; | ||||||
|
||||||
import 'package:path/path.dart' as p; | ||||||
|
||||||
import '../../melos.dart'; | ||||||
import '../common/utils.dart'; | ||||||
import 'base.dart'; | ||||||
|
||||||
class InitCommand extends MelosCommand { | ||||||
InitCommand(super.config) { | ||||||
argParser.addOption( | ||||||
'directory', | ||||||
abbr: 'd', | ||||||
help: 'Directory to create project in. Defaults to the workspace name.', | ||||||
); | ||||||
|
||||||
argParser.addMultiOption( | ||||||
'packages', | ||||||
abbr: 'p', | ||||||
help: 'Comma separated glob paths to add to the melos workspace.', | ||||||
); | ||||||
|
||||||
argParser.addOption( | ||||||
'project', | ||||||
abbr: 'P', | ||||||
help: 'Project name to be used in pubspec.yaml. ' | ||||||
'Defaults to the workspace name.', | ||||||
); | ||||||
} | ||||||
|
||||||
@override | ||||||
final String name = 'init'; | ||||||
|
||||||
@override | ||||||
final String description = 'Initialize a new Melos workspace.'; | ||||||
|
||||||
@override | ||||||
Future<void> run() { | ||||||
final workspaceDefault = p.basename(Directory.current.absolute.path); | ||||||
final workspaceName = argResults!.rest.firstOrNull ?? | ||||||
promptInput( | ||||||
'Enter your workspace name', | ||||||
defaultsTo: workspaceDefault, | ||||||
); | ||||||
final directory = argResults!['directory'] as String? ?? | ||||||
promptInput( | ||||||
'Enter the directory', | ||||||
defaultsTo: workspaceDefault != workspaceName ? workspaceName : '.', | ||||||
); | ||||||
final packages = argResults!['packages'] as List<String>?; | ||||||
final project = argResults!['project'] as String? ?? | ||||||
promptInput( | ||||||
'Enter the project name', | ||||||
defaultsTo: workspaceName, | ||||||
); | ||||||
final useAppsDir = promptBool( | ||||||
message: 'Do you want to add the apps directory to the list of packages?', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We are actually creating the |
||||||
defaultsTo: true, | ||||||
); | ||||||
|
||||||
final melos = Melos(logger: logger, config: config); | ||||||
|
||||||
return melos.init( | ||||||
workspaceName, | ||||||
directory: directory, | ||||||
packages: packages ?? const [], | ||||||
project: project, | ||||||
useAppDir: useAppsDir, | ||||||
); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,63 @@ | ||||||
part of 'runner.dart'; | ||||||
|
||||||
mixin _InitMixin on _Melos { | ||||||
Future<void> init( | ||||||
String workspaceName, { | ||||||
required String directory, | ||||||
required List<String> packages, | ||||||
required String project, | ||||||
bool useAppDir = false, | ||||||
}) async { | ||||||
late final String qualifiedWorkspaceName; | ||||||
if (workspaceName == '.') { | ||||||
qualifiedWorkspaceName = p.basename(Directory.current.absolute.path); | ||||||
} else { | ||||||
qualifiedWorkspaceName = workspaceName; | ||||||
} | ||||||
|
||||||
final isCurrentDir = directory == '.'; | ||||||
final dir = Directory(directory); | ||||||
if (!isCurrentDir && dir.existsSync()) { | ||||||
throw StateError('Directory $directory already exists'); | ||||||
} else if (!isCurrentDir) { | ||||||
dir.createSync(recursive: true); | ||||||
Directory(p.join(dir.absolute.path, 'packages')).createSync(); | ||||||
if (useAppDir) { | ||||||
Directory(p.join(dir.absolute.path, 'apps')).createSync(); | ||||||
} | ||||||
} | ||||||
|
||||||
final dartVersion = utils.currentDartVersion('dart'); | ||||||
final melosYaml = <String, Object?>{ | ||||||
'name': qualifiedWorkspaceName, | ||||||
'packages': [if (useAppDir) 'apps/*', 'packages/*'], | ||||||
if (packages.isNotEmpty) 'packages': packages, | ||||||
}; | ||||||
final pubspecYaml = <String, dynamic>{ | ||||||
exaby73 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
'name': project, | ||||||
'environment': { | ||||||
'sdk': '>=$dartVersion <${dartVersion.major + 1}.0.0', | ||||||
}, | ||||||
'dev_dependencies': { | ||||||
'melos': '^$melosVersion', | ||||||
}, | ||||||
}; | ||||||
|
||||||
final melosFile = File(p.join(dir.absolute.path, 'melos.yaml')); | ||||||
final pubspecFile = File(p.join(dir.absolute.path, 'pubspec.yaml')); | ||||||
|
||||||
melosFile.writeAsStringSync( | ||||||
(YamlEditor('')..update([], melosYaml)).toString(), | ||||||
); | ||||||
pubspecFile.writeAsStringSync( | ||||||
(YamlEditor('')..update([], pubspecYaml)).toString(), | ||||||
); | ||||||
|
||||||
logger.log( | ||||||
'Initialized Melos workspace in ${dir.path}.\n' | ||||||
'Run the following commands to bootstrap the workspace:\n' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Since this won't do anything when run directly after |
||||||
' cd ${dir.path}\n' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the |
||||||
' melos bootstrap', | ||||||
); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -17,6 +17,7 @@ import 'package:pubspec/pubspec.dart'; | |||
import 'package:yaml/yaml.dart'; | ||||
import 'package:yaml_edit/yaml_edit.dart'; | ||||
|
||||
import '../../version.g.dart'; | ||||
spydon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
import '../command_configs/command_configs.dart'; | ||||
import '../command_runner/version.dart'; | ||||
import '../common/aggregate_changelog.dart'; | ||||
|
@@ -52,6 +53,7 @@ part 'run.dart'; | |||
part 'version.dart'; | ||||
part 'analyze.dart'; | ||||
part 'format.dart'; | ||||
part 'init.dart'; | ||||
|
||||
enum CommandWithLifecycle { | ||||
bootstrap, | ||||
|
@@ -70,7 +72,8 @@ class Melos extends _Melos | |||
_VersionMixin, | ||||
_PublishMixin, | ||||
_AnalyzeMixin, | ||||
_FormatMixin { | ||||
_FormatMixin, | ||||
_InitMixin { | ||||
Melos({ | ||||
required this.config, | ||||
Logger? logger, | ||||
|
@@ -84,6 +87,7 @@ class Melos extends _Melos | |||
|
||||
abstract class _Melos { | ||||
MelosLogger get logger; | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
MelosWorkspaceConfig get config; | ||||
|
||||
Future<MelosWorkspace> createWorkspace({ | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that we need to differentiate between workspace name and project name