Skip to content

Commit

Permalink
Basic app structure and Grunt build modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource committed Sep 8, 2012
1 parent c344126 commit f47cde7
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 20 deletions.
45 changes: 36 additions & 9 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ module.exports = function (grunt) {
lint:{
files:['grunt.js', 'src/**/*.js', 'test/unit/**/*.js']
},
test:{
files:['test/unit/**/*.js']
},
concat:{
dist:{
src:['<banner:meta.banner>', 'src/**/*.js'],
Expand All @@ -31,8 +28,8 @@ module.exports = function (grunt) {
}
},
watch:{
files:'<config:lint.files>',
tasks:'lint test'
files:['<config:lint.files>', 'src/**/*.html'],
tasks:'lint test concat min concatPartials index'
},
jshint:{
options:{
Expand All @@ -51,16 +48,20 @@ module.exports = function (grunt) {
uglify:{}
});

grunt.registerTask('index', 'Process index.html', function(){
grunt.file.copy('src/index.html', 'dist/index.html', {process:grunt.template.process});
});

// Default task.
grunt.registerTask('default', 'lint concat min');
grunt.registerTask('default', 'lint test concat min concatPartials index');

grunt.registerTask('server', 'start testacular server', function () {
//Mark the task as async but never call done, so the server stays up
var done = this.async();
testacular.server.start({configFile : 'test/config/test-config.js'});
testacular.server.start({configFile:'test/config/test-config.js'});
});

grunt.registerTask('test', 'start testacular test', function () {
grunt.registerTask('test', 'run testacular tests', function () {

var specDone = this.async();
var testacularCmd = process.platform === 'win32' ? 'testacular.cmd' : 'testacular';
Expand All @@ -76,4 +77,30 @@ module.exports = function (grunt) {
child.stderr.pipe(process.stderr);
});

};
grunt.registerTask('concatPartials', 'concat partials', function () {
//TODO: horrible implementation, to be fixed, but this Grunt task makes sense for the AngularJS community!
var content = '', partials = grunt.file.expandFiles('src/modules/*/partials/**/*.tpl.html');
for (var i=0; i<partials.length; i++){
var partialFile = partials[i];
var partialel = partialFile.split('/');
var partial = "<script type='text/ng-template' id='"+partialel[partialel.length-1]+"'>"+grunt.file.read(partialFile)+"</script>\n";
content += partial;
}
grunt.file.write('dist/partials.tpl.html', content);
});

grunt.registerTask('module', 'create new module', function () {
var moduleName = this.args[0];
grunt.log.debug("Creating new module: " + moduleName);
var srcPath = 'src/modules/' + moduleName + '/';
var testPath = 'test/modules/' + moduleName + '/';

//js
//main module file
grunt.file.write(srcPath + moduleName + '.js', "angular.module('" + moduleName + "',[]);\nangular.module('" + moduleName + "').controller('" + moduleName + "Controller', function($scope){\n});");
//tests
grunt.file.write(testPath + '/unit/' + moduleName + 'Spec.js', "//jasmine template here");
//partials
grunt.file.write(srcPath + 'partials/' + moduleName + '.tpl.html', '<div>' + moduleName + '</div>');
});
};
4 changes: 0 additions & 4 deletions src/app.js

This file was deleted.

15 changes: 11 additions & 4 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<html>
<html ng-app="app">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js"></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript" src="angular-app.min.js"></script>

<!-- partials start -->
<%= grunt.file.read('dist/partials.tpl.html') %>
<!-- partials end -->

</head>
<body ng-app="app" ng-controller="MainController">
Hello, {{hello}}!
<body ng-controller="AppController">
Hello, {{user || 'Anonymous'}}! You are here: {{location.path()}}
<hr>
<div ng-view></div>
</body>
</html>
11 changes: 11 additions & 0 deletions src/modules/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
angular.module('app', ['signin', 'dashboard']);

angular.module('app').config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/signin', {templateUrl:'signin.form.tpl.html', controller:'SignInController'});
$routeProvider.when('/dashboard', {templateUrl:'dashboard.tpl.html', controller:'DashboardController'});
$routeProvider.otherwise({redirectTo:'/signin'});
}]);

angular.module('app').controller('AppController', ['$scope', '$location', function ($scope, $location) {
$scope.location = $location;
}]);
3 changes: 3 additions & 0 deletions src/modules/dashboard/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
angular.module('dashboard',[]);
angular.module('dashboard').controller('DashboardController', ['$scope', function($scope){
}]);
1 change: 1 addition & 0 deletions src/modules/dashboard/partials/dashboard.tpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>Dashboard!</div>
5 changes: 5 additions & 0 deletions src/modules/signin/partials/signin.form.tpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<form name="signin">
login: <input name="login" ng-model="user.login"><br>
pass: <input name="pass" ng-model="user.pass"><br>
<a href="#/dashboard">Sign in</a>
</form>
4 changes: 4 additions & 0 deletions src/modules/signin/signin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
angular.module('signin', []);
angular.module('signin').controller('SignInController', ['$scope', function($scope){
$scope.user = {};
}]);
2 changes: 1 addition & 1 deletion test/config/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ files = [
'test/lib/angular/angular.js',
'test/lib/angular/angular-mocks.js',
'src/**/*.js',
'test/unit/**/*Spec.js'
'test/**/unit/**/*Spec.js'
];

// list of files to exclude
Expand Down
4 changes: 2 additions & 2 deletions test/unit/appSpec.js → test/modules/app/unit/appSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('hello world scenario', function () {
}));

it('should say hello to a hello in scope', function () {
$controller('MainController', {$scope:scope});
expect(scope.hello).toEqual('World');
$controller('AppController', {$scope:scope});
expect(scope.user).toBeUndefined();
});
});
1 change: 1 addition & 0 deletions test/modules/dashboard/unit/dashboardSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//jasmine template here
7 changes: 7 additions & 0 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- There is no npm module for AngularJS, so its version is hard-coded, libs checked-in (for tests) etc.

Nice to have
-----------------------------------------------------------------------
- grunt task to:
- create new modules
- concat partials (globally or per-module?)

0 comments on commit f47cde7

Please sign in to comment.