Skip to content
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

Implement #2 #13

Merged
merged 5 commits into from
Jan 26, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions lib/src/commands/spec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

library den.src.commands.spec;

import 'dart:async';
import 'dart:mirrors';
import 'dart:io';

import 'package:path/path.dart' as p;
import 'package:unscripted/unscripted.dart';
import 'package:prompt/prompt.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart';

import '../pub.dart';
import '../theme.dart';

class SpecCommand {

Pubspec _pubspec;
InstanceMirror get _pubspecReflection => reflect(_pubspec);
YamlMap get _yamlMap => _pubspec.yamlMap;
final fields = ['name','version','author','homepage','description'];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed dart editor has author before version, let's switch to that to match.


List<Question> get questions =>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still used?

fields.map((String field){
return new Question("$field (${_yamlMap[field]})");
});

@SubCommand(help: 'Initiate or edit pubspec.yaml file.')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"pubspec.yaml file" -> "a pubspec"

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Initiate" -> "Initialize"

spec(

) {
_prompts();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: not sure why _prompts() is a separate method.

}

Future _prompts() {
var pubspecFile = new File('pubspec.yaml');
_pubspec = pubspecFile.existsSync() ? Pubspec.load() : Pubspec.init();

return Future.forEach(
fields,
(String field){
var defaultValue = _yamlMap[field] != null ? _yamlMap[field] : '';

var question = defaultValue != ''
? new Question("${theme.question(field)} (${theme.questionDefault(defaultValue)}):", defaultsTo: defaultValue)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the ":", it should already be included in the prompt output.

: new Question(theme.question(field), defaultsTo: '');

return ask(question)
.then((String response){
if(response=='') _pubspecReflection.setField(new Symbol(field), defaultValue);
else _pubspecReflection.setField(new Symbol(field), response);
});
}
).whenComplete((){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whenComplete -> then (otherwise errors are swallowed)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for other uses of whenComplete


var contents = _syntaxHighlighted();
print("\npubspec.yaml\n============\n$contents\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print('''

pubspec.yaml
===========

$contents
''');

return ask(new Question.confirm("All correct?", defaultsTo:true))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the "?" (there's already one in the left column)

.then((bool correct){
if(!correct) return _prompts();
else {
_pubspec.save();
return close();
}
});
});
}

String _syntaxHighlighted() {
var fieldContents = fields.toSet().intersection(_yamlMap.keys.toSet()).map(
(String field) {
return "${theme.field(field)}: ${theme.value(_yamlMap[field])}";
}
).join("\n");

var envContents = _yamlMap['environment'] != null ? _block('environment') : '';
var depContents = _yamlMap['dependencies'] != null ? _block('dependencies') : '';
var devdepContents = _yamlMap['dev_dependencies'] != null ? _block('dev_dependencies') : '';;

return "$fieldContents$envContents$depContents$devdepContents";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can assume this order or that there are not other fields to include. I think it needs to go through the contents of the _yamlMap and generically highlight things. I'd be ok with saving the semantic highlighting for later though.

}

String _block(String title) {
var lines = _yamlMap[title].keys
.map((String key) {
var value = "'${_yamlMap[title][key]}'";
return "${theme.dependency(key)}: ${theme.value(value)}";
}).toList();
return "\n${theme.field(title + ':')}\n${lines.map((line) => ' $line').join('\n')}";
}
}
3 changes: 2 additions & 1 deletion lib/src/den.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import 'commands/bump.dart';
import 'commands/fetch.dart';
import 'commands/install.dart';
import 'commands/pull.dart';
import 'commands/spec.dart';
import 'commands/uninstall.dart';

class Den extends Object with BumpCommand, FetchCommand, InstallCommand, PullCommand , UninstallCommand {
class Den extends Object with BumpCommand, FetchCommand, SpecCommand, InstallCommand, PullCommand , UninstallCommand {

@Command(
allowTrailingOptions: true,
Expand Down
5 changes: 5 additions & 0 deletions lib/src/git.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:pub_semver/pub_semver.dart';
import 'package:which/which.dart';

Future<ProcessResult> runGit(args, {String workingDirectory}) => Process.run('git', args, workingDirectory: workingDirectory);
ProcessResult runGitSync(args, {String workingDirectory}) => Process.runSync('git', args, workingDirectory: workingDirectory);

const String _versionTemplate = 'v{v}';

Expand Down Expand Up @@ -58,10 +59,14 @@ ${status.join("\n")}''');
Future<bool> checkHasGit() =>
which('git', orElse: () => null).then((git) => git != null);

bool checkHasGitSync() => whichSync('git', orElse: () => null) != null;

Future<List<String>> gitStatus() => runGit(['status', '--porcelain']).then((processResult) {
var lines = processResult.stdout.trim().split("\n")
.where((String line) => line.trim().isNotEmpty && !line.startsWith('?? '))
.map((line) => line.trim());
return lines;
});

String gitConfigUserNameSync() => runGitSync(['config', 'user.name']).stdout.trim();
String gitConfigUserEmailSync() => runGitSync(['config', 'user.email']).stdout.trim();
34 changes: 34 additions & 0 deletions lib/src/pub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart';

import 'yaml_edit.dart';
import 'git.dart';


class Pubspec {

Expand All @@ -25,14 +27,33 @@ class Pubspec {
String get path => _path;
final String _path;
String get name => _yamlMap['name'];
set name(String _name) {
contents = setMapKey(_contents, _yamlMap, 'name', _name, false);
}
String get author => _yamlMap['author'];
set author(String _author) {
_setValue('author', _author);
}
Version get version => new Version.parse(_yamlMap['version']);
set version(Version v) {
contents = setMapKey(_contents, _yamlMap, 'version', v.toString(), false);
}
String get homepage => _yamlMap['homepage'];
set homepage(String _homepage) {
_setValue('homepage', _homepage);
}
String get documentation => _yamlMap['documentation'];
String get description => _yamlMap['description'];
set description(String _description) {
_setValue('description', _description);
}
void _setValue(String key, String value) {
if(value!=null && value!='') {
contents = setMapKey(_contents, _yamlMap, key, value, false);
} else {
contents = deleteMapKey(_contents, _yamlMap, key);
}
}
VersionConstraint get sdkConstraint {
var env = _yamlMap['environment'];
if (env == null) return VersionConstraint.any;
Expand Down Expand Up @@ -87,6 +108,19 @@ class Pubspec {
this._contents,
this._yamlMap);

static Pubspec init() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this return a Future<Pubspec> and use the async git methods. Reason is I'm considering calling the github API to get the description later on, which would have to be async.

var packageRoot = p.current;
var pubspecPath = p.join(packageRoot, _PUBSPEC);
var contents = "name: ${p.basename(packageRoot)}";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to strip off any ".dart" or "-dart" suffix here.

var yaml = loadYamlNode(contents, sourceUrl: pubspecPath);
var _pubspec = new Pubspec(pubspecPath, contents, yaml);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the "_", no need to make a local variable private.


_pubspec.version = new Version.parse('0.1.0');

if(checkHasGitSync()) _pubspec.author = "${gitConfigUserNameSync()} <${gitConfigUserEmailSync()}>";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need some error handling here. If git fails, or if the git config for either field was not set, it should not set the author.


return _pubspec;
}
static Pubspec load([String path]) {
var packageRoot = _getPackageRoot(path == null ? p.current : path);
var pubspecPath = p.join(packageRoot, _PUBSPEC);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class DenTheme {
final AnsiPen info = new AnsiPen()..white();
final AnsiPen warning = new AnsiPen()..yellow();
final AnsiPen error = new AnsiPen()..red(bold: true);
final AnsiPen question = new AnsiPen()..white();
final AnsiPen questionDefault = new AnsiPen()..green(bold: true);
final AnsiPen field = new AnsiPen()..green(bold: true);
final AnsiPen value = new AnsiPen()..blue(bold:true);
}

final theme = new DenTheme();
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
unscripted: '>=0.5.0 <0.6.0'
which: '>=0.1.0 <0.2.0'
yaml: '>=2.0.1+1 <3.0.0'
prompt: '>=0.1.3 <0.2.0'
dev_dependencies:
unittest: '>=0.11.0 <12.0.0'
executables:
Expand Down