Skip to content

Commit

Permalink
📝 add code doc comments
Browse files Browse the repository at this point in the history
Signed-off-by: birjuvachhani <[email protected]>
  • Loading branch information
BirjuVachhani committed Feb 3, 2020
1 parent ada871e commit 548e358
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.1.0

- added dart class generator
- fix pub.dev warnings
- add code documentation

## 0.0.1

- pre-alpha release
Expand Down
1 change: 1 addition & 0 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:args/args.dart';
import 'package:path/path.dart' as path;
import 'package:spider/spider.dart';

/// Handles all the commands
void main(List<String> arguments) {
var pubspec_path = path.join(Directory.current.path, 'pubspec.yaml');
if (!File(pubspec_path).existsSync()) {
Expand Down
11 changes: 5 additions & 6 deletions lib/spider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import 'package:spider/src/dart_class_generator.dart';

import 'src/utils.dart';

/// Entry point of all the command process
/// provides varios functions to execute commands
class Spider {
final String _path;
Configuration configs;
Expand All @@ -34,15 +36,10 @@ class Spider {
caseSensitive: false);

Spider(this._path) {
_initialize();
configs = Configuration(_path);
}

void _initialize() async {
if (!await Directory(_path).exists()) {}
}

void listen_for_changes() {
void _listen_for_changes() {
print('Watching for changes in directory ${configs["path"]}...');
Directory(configs['path'])
.watch(events: FileSystemEvent.all)
Expand All @@ -55,6 +52,7 @@ class Spider {
});
}

/// generates dart code for given [configs] parsed from spider.yaml
void generate_code() {
var properties = createFileMap(configs['path']);
var generator = DartClassGenerator(
Expand All @@ -75,6 +73,7 @@ class Spider {
processing = false;
}

/// initializes config file (spider.yaml) in the root of the project
static void init_configs() async {
var configFile = File(Constants.CONFIG_FILE_NAME);
await configFile.writeAsString(Constants.DEFAULT_CONFIGS_STRING);
Expand Down
4 changes: 3 additions & 1 deletion lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Configuration {
_initialize();
}

/// loads spider.yaml file
void _initialize() {
if (!Directory(_path).existsSync()) {
print('$_path does not exists!');
Expand All @@ -50,5 +51,6 @@ class Configuration {
}
}

operator [](String name) => _configs[name] ?? _defaults[name];
/// allows to access yaml data directly
dynamic operator [](String name) => _configs[name] ?? _defaults[name];
}
1 change: 1 addition & 0 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// Author: Birju Vachhani
// Created Date: February 02, 2020

/// Holds all the constants
class Constants {
static final String CONFIG_FILE_NAME = 'spider.yaml';
static final String DEFAULT_CONFIGS_STRING =
Expand Down
11 changes: 7 additions & 4 deletions lib/src/dart_class_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import 'package:meta/meta.dart';

/// Generates dart class code using given data
class DartClassGenerator {
final String className;
bool useStatic = false;
Expand All @@ -35,11 +36,12 @@ class DartClassGenerator {
DartClassGenerator(
{@required this.className,
@required this.properties,
this.use_underscores,
this.useConst,
this.useStatic,
this.prefix});
this.use_underscores = false,
this.useConst = true,
this.useStatic = true,
this.prefix = ''});

/// generates dart class code and returns it as a single string
String generate() {
var properties_strings = properties.keys.map<String>((name) {
var str = useStatic ? '\tstatic ' : '\t';
Expand Down Expand Up @@ -82,5 +84,6 @@ ${properties_strings.join('\n')}
(match) => match.group(2).toUpperCase());
}

/// formats path string to match with flutter's standards
String formatPath(String value) => value.replaceAll('\\', '/');
}
3 changes: 3 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import 'package:path/path.dart' as p;

import 'constants.dart';

/// Returns an instance of [File] if given [path] exists, null otherwise.
File file(String path) {
var file = File(path);
return file.existsSync() ? file : null;
}

/// formats file name according to effective dart
String formatFileName(String name) {
name = name
.replaceAllMapped(
Expand All @@ -36,6 +38,7 @@ String formatFileName(String name) {
return name.contains('.dart') ? name : name + '.dart';
}

/// Writes given [content] to the file with given [name] at given [path].
void writeToFile({String name, String path, String content}) {
if (!Directory(p.join(Constants.LIB_FOLDER, path)).existsSync()) {
Directory(p.join(Constants.LIB_FOLDER, path)).createSync(recursive: true);
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: spider
description: A small dart library to generate Assets dart code from assets folder. It generates dart class with static const variables in it which can be used to reference the assets safely anywhere in the flutter app.
version: 0.0.1
version: 0.1.0
homepage: https://github.com/BirjuVachhani/spider

environment:
Expand All @@ -11,6 +11,7 @@ dependencies:
intl: ^0.16.1
yaml: ^2.2.0
args: ^1.5.2
meta: ^1.1.8

dev_dependencies:
pedantic: ^1.8.0
Expand Down
36 changes: 34 additions & 2 deletions test/spider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,42 @@
// Author: Birju Vachhani
// Created Date: February 02, 2020

import 'package:spider/src/dart_class_generator.dart';
import 'package:test/test.dart';

/// runs all the test cases
void main() {
test('calculate', () {
// expect(calculate(), 42);
test('format name test', () {
var generator = DartClassGenerator(className: '', properties: {});
expect(generator.formatName('test_case'), 'testCase');
expect(generator.formatName('test case'), 'testCase');
expect(generator.formatName('Test Case'), 'testCase');
expect(generator.formatName('__Test Case'), 'testCase');
expect(generator.formatName('Test Case'), 'testCase');
expect(generator.formatName('Test@)(Case'), 'testCase');
expect(generator.formatName('TestCase'), 'testCase');
expect(generator.formatName('test-case-with some_word'),
'testCaseWithSomeWord');
generator = DartClassGenerator(
className: '', properties: {}, use_underscores: true);
expect(generator.formatName('test_case'), 'test_case');
expect(generator.formatName('test case'), 'test_case');
expect(generator.formatName('Test Case'), 'test_case');
expect(generator.formatName('__Test Case'), 'test_case');
expect(generator.formatName('Test Case'), 'test_case');
expect(generator.formatName('Test@)(Case'), 'test_case');
expect(generator.formatName('TestCase'), 'test_case');
expect(generator.formatName('test-case-with some_word'),
'test_case_with_some_word');
});

test('format path test', () {
var generator = DartClassGenerator(className: '', properties: {});
expect(generator.formatPath('assets\\abc.png'), 'assets/abc.png');
expect(generator.formatPath('assets/temp.png'), 'assets/temp.png');
expect(
generator.formatPath('assets/temp/temp.png'), 'assets/temp/temp.png');
expect(
generator.formatPath('assets\\temp\\temp.png'), 'assets/temp/temp.png');
});
}

0 comments on commit 548e358

Please sign in to comment.