Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- module.exports = function(grunt)
- {
- // Load modules.
- var fs = require('fs');
- var path = require('path');
- var S = require('string');
- var sassCachePath = path.join(__dirname, '.sass-cache');
- var sassCachePathForSass = S(sassCachePath).replaceAll('\\', '/');
- var compassPlugins = ['susy', 'breakpoint', 'breakpoint-slicer'];
- // All the extensions we consider being assets.
- var assetExtensions = ['.js', '.css', '.map', '.scss', '.svg', '.eot', '.woff', '.ttf', '.png', '.jpg', '.gif'];
- // Single expander for asset.
- var expandAsset = function(asset, expansion)
- {
- return expansion + asset;
- };
- // Expander for multiple assets (array).
- var expandAssets = function(assetExtensions, expansion)
- {
- return assetExtensions.map(function(asset)
- {
- return expandAsset(asset, expansion);
- });
- };
- // Extract module name from StoneOS project.
- var getModuleName = function(source)
- {
- // Module's full name parts.
- var moduleFullName = source.split('.');
- // Get last element.
- var module = moduleFullName.pop();
- return module;
- };
- var gruntConfig =
- {
- pkg: grunt.file.readJSON('package.json'),
- msbuild:
- {
- development:
- {
- src: ['src/StoneOS.Runner/StoneOS.Runner.csproj'],
- options:
- {
- projectConfiguration: 'Debug',
- targets: ['Clean', 'Rebuild'],
- version: 4.0,
- maxCpuCount: 4,
- buildParameters:
- {
- WarningLevel: 2,
- OutputPath : __dirname + '/build/Development'
- },
- verbosity: 'quiet'
- }
- },
- release :
- {
- src: ['src/StoneOS.Runner/StoneOS.Runner.csproj'],
- options:
- {
- projectConfiguration: 'Release',
- targets: ['Clean', 'Rebuild'],
- version: 4.0,
- maxCpuCount: 4,
- buildParameters:
- {
- WarningLevel: 2,
- OutputPath : __dirname + '/build/Release'
- },
- verbosity: 'quiet'
- }
- }
- },
- compass:
- {},
- vsembed:
- {},
- vsrembed:
- {},
- copy:
- {
- developmentAssets:
- {
- expand : true,
- cwd : 'src/',
- src : expandAssets(assetExtensions, '*/assets/**/*'),
- dest : 'build/Development/public/assets/',
- filter : 'isFile',
- rename : function(destination, source)
- {
- // Get the directory tree.
- var tree = source.split('/');
- // Get module name.
- var module = getModuleName(tree[0]).toLowerCase();
- // Remove first two entries.
- var targetTree = tree.slice(2);
- // Add module.
- targetTree.unshift(module);
- return path.join(destination, targetTree.join('/'));
- }
- },
- releaseAssets:
- {
- expand : true,
- cwd : 'src/',
- src : expandAssets(assetExtensions, '*/assets/**/*'),
- dest : 'build/Release/public/assets/',
- filter : 'isFile',
- rename : function(destination, source)
- {
- // Get the directory tree.
- var tree = source.split('/');
- // Get module name.
- var module = getModuleName(tree[0]).toLowerCase();
- // Remove first two entries.
- var targetTree = tree.slice(2);
- // Add module.
- targetTree.unshift(module);
- return path.join(destination, targetTree.join('/'));
- }
- },
- developmentViews:
- {
- expand : true,
- cwd : 'src/',
- src : '*/views/**',
- dest : 'build/Development/views/',
- filter : 'isFile',
- rename : function(destination, source)
- {
- // Get the directory tree.
- var tree = source.split('/');
- // Remove first two entries.
- var targetTree = tree.slice(2);
- return path.join(destination, targetTree.join('/'));
- }
- },
- releaseViews:
- {
- expand : true,
- cwd : 'src/',
- src : '*/views/**',
- dest : 'build/Release/views/',
- filter : 'isFile',
- rename : function(destination, source)
- {
- // Get the directory tree.
- var tree = source.split('/');
- // Remove first two entries.
- var targetTree = tree.slice(2);
- return path.join(destination, targetTree.join('/'));
- }
- }
- },
- clean :
- {
- developmentBuild :
- {
- src : 'build/Development'
- },
- releaseBuild :
- {
- src : 'build/Release'
- }
- }
- };
- // Add compass, vsembed, vsrembed targets.
- grunt.file.expand({filter : 'isFile'}, 'src/StoneOS.*/*.csproj').forEach(function (destination)
- {
- // Project file.
- var projectName = path.basename(destination, '.csproj');
- var moduleName = getModuleName(projectName);
- // Extract directory.
- var directory = path.dirname(destination);
- directory = path.resolve(directory);
- // Generate assets path.
- var assetsPath = path.join(directory, 'assets');
- var assetsPathForSass = S(assetsPath).replaceAll('\\', '/');
- // Sass path.
- var sassPath = path.join(assetsPath, 'sass');
- // Look up if Sass assets exists.
- var assetsExists = fs.existsSync(sassPath);
- var assetCount = assetsExists ? grunt.file.expand({ filter : 'isFile', cwd : sassPath }, '**/*.scss').length : 0;
- // grunt.log.writeln('Assets for', projectName, ':', assetCount);
- if (assetCount > 0)
- {
- var cssPath = path.join(assetsPath, 'styles');
- var fontsPath = path.join(assetsPath, 'fonts');
- var imagesPath = path.join(assetsPath, 'images');
- var javascriptsPath = path.join(assetsPath, 'scripts');
- gruntConfig.compass[projectName] =
- {
- options :
- {
- httpPath : '/assets/' + moduleName.toLowerCase() + '/',
- sassDir : sassPath,
- relativeAssets : true,
- noLineComments : true,
- outputStyle : 'compressed',
- cssDir : cssPath,
- fontsDir : fontsPath,
- imagesDir : imagesPath,
- javascriptsDir : javascriptsPath,
- require : compassPlugins,
- raw : [
- 'cache_path = "'+ sassCachePathForSass +'"',
- 'project_path = "'+ assetsPathForSass + '"',
- 'sass_options = {:sourcemap => true}'
- ].join('\n') + '\n'
- }
- };
- var embedRembedAssetTask =
- {
- options:
- {
- project: directory,
- directories: ['assets'],
- extensions: assetExtensions
- }
- };
- gruntConfig.vsembed[projectName + '.assets'] = embedRembedAssetTask;
- gruntConfig.vsrembed[projectName + '.assets'] = embedRembedAssetTask;
- grunt.verbose.writeln('Registering \'asset\' embed/rembed task for', projectName);
- }
- // Where views should be located.
- var viewsPath = path.join(directory, 'views');
- // Look up if views exist.
- var viewsExists = fs.existsSync(viewsPath);
- var viewCount = viewsExists ? grunt.file.expand({ filter : 'isFile', cwd : viewsPath }, '**/*.cshtml').length : 0;
- // grunt.log.writeln('Views for', projectName, ':', viewCount);
- if (viewCount > 0)
- {
- var embedRembedViewsTask =
- {
- options:
- {
- project: directory,
- directories: ['views'],
- extensions: '.cshtml'
- }
- };
- gruntConfig.vsembed[projectName + '.views'] = embedRembedViewsTask;
- gruntConfig.vsrembed[projectName + '.views'] = embedRembedViewsTask;
- grunt.verbose.writeln('Registering \'view\' embed/rembed task for', projectName);
- }
- });
- grunt.initConfig(gruntConfig);
- // Local tasks.
- grunt.loadTasks('grunt_tasks');
- // NPM tasks.
- grunt.loadNpmTasks('grunt-msbuild');
- grunt.loadNpmTasks('grunt-contrib-compass');
- grunt.loadNpmTasks('grunt-contrib-copy');
- grunt.loadNpmTasks('grunt-contrib-clean');
- // Task groups.
- grunt.registerTask('assets', ['compass']);
- grunt.registerTask('embed', ['vsembed']);
- grunt.registerTask('rembed', ['vsrembed']);
- grunt.registerTask('development', ['clean:developmentBuild', 'assets', 'copy:developmentAssets', 'copy:developmentViews', 'rembed', 'msbuild:development']);
- grunt.registerTask('release', ['clean:releaseBuild', 'assets', 'embed', 'msbuild:release']);
- grunt.registerTask('default', function()
- {
- grunt.log.writeln('Nothing to do...');
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement