Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. module.exports = function(grunt)
  4. {
  5.     // Load modules.
  6.     var fs = require('fs');
  7.     var path = require('path');
  8.     var S = require('string');
  9.  
  10.     var sassCachePath = path.join(__dirname, '.sass-cache');
  11.     var sassCachePathForSass = S(sassCachePath).replaceAll('\\', '/');
  12.  
  13.     var compassPlugins = ['susy', 'breakpoint', 'breakpoint-slicer'];
  14.  
  15.     // All the extensions we consider being assets.
  16.     var assetExtensions = ['.js', '.css', '.map', '.scss', '.svg', '.eot', '.woff', '.ttf', '.png', '.jpg', '.gif'];
  17.  
  18.     // Single expander for asset.
  19.     var expandAsset = function(asset, expansion)
  20.     {
  21.         return expansion + asset;
  22.     };
  23.  
  24.     // Expander for multiple assets (array).
  25.     var expandAssets = function(assetExtensions, expansion)
  26.     {
  27.         return assetExtensions.map(function(asset)
  28.         {
  29.             return expandAsset(asset, expansion);
  30.         });
  31.     };
  32.  
  33.     // Extract module name from StoneOS project.
  34.     var getModuleName = function(source)
  35.     {
  36.         // Module's full name parts.
  37.         var moduleFullName = source.split('.');
  38.  
  39.         // Get last element.
  40.         var module = moduleFullName.pop();
  41.  
  42.         return module;
  43.     };
  44.  
  45.     var gruntConfig =
  46.     {
  47.         pkg: grunt.file.readJSON('package.json'),
  48.  
  49.         msbuild:
  50.         {
  51.             development:
  52.             {
  53.                 src: ['src/StoneOS.Runner/StoneOS.Runner.csproj'],
  54.                 options:
  55.                 {
  56.                     projectConfiguration: 'Debug',
  57.                     targets: ['Clean', 'Rebuild'],
  58.                     version: 4.0,
  59.                     maxCpuCount: 4,
  60.                     buildParameters:
  61.                     {
  62.                         WarningLevel: 2,
  63.                         OutputPath : __dirname + '/build/Development'
  64.                     },
  65.                     verbosity: 'quiet'
  66.                 }
  67.             },
  68.  
  69.             release :
  70.             {
  71.                 src: ['src/StoneOS.Runner/StoneOS.Runner.csproj'],
  72.                 options:
  73.                 {
  74.                     projectConfiguration: 'Release',
  75.                     targets: ['Clean', 'Rebuild'],
  76.                     version: 4.0,
  77.                     maxCpuCount: 4,
  78.                     buildParameters:
  79.                     {
  80.                         WarningLevel: 2,
  81.                         OutputPath : __dirname + '/build/Release'
  82.                     },
  83.                     verbosity: 'quiet'
  84.                 }
  85.             }
  86.         },
  87.  
  88.         compass:
  89.         {},
  90.  
  91.         vsembed:
  92.         {},
  93.  
  94.         vsrembed:
  95.         {},
  96.  
  97.         copy:
  98.         {
  99.             developmentAssets:
  100.             {
  101.                 expand : true,
  102.                 cwd : 'src/',
  103.                 src : expandAssets(assetExtensions, '*/assets/**/*'),
  104.                 dest : 'build/Development/public/assets/',
  105.                 filter : 'isFile',
  106.                 rename : function(destination, source)
  107.                 {
  108.                     // Get the directory tree.
  109.                     var tree = source.split('/');
  110.  
  111.                     // Get module name.
  112.                     var module = getModuleName(tree[0]).toLowerCase();
  113.  
  114.                     // Remove first two entries.
  115.                     var targetTree = tree.slice(2);
  116.  
  117.                     // Add module.
  118.                     targetTree.unshift(module);
  119.  
  120.                     return path.join(destination, targetTree.join('/'));
  121.                 }
  122.             },
  123.  
  124.             releaseAssets:
  125.             {
  126.                 expand : true,
  127.                 cwd : 'src/',
  128.                 src : expandAssets(assetExtensions, '*/assets/**/*'),
  129.                 dest : 'build/Release/public/assets/',
  130.                 filter : 'isFile',
  131.                 rename : function(destination, source)
  132.                 {
  133.                     // Get the directory tree.
  134.                     var tree = source.split('/');
  135.  
  136.                     // Get module name.
  137.                     var module = getModuleName(tree[0]).toLowerCase();
  138.  
  139.                     // Remove first two entries.
  140.                     var targetTree = tree.slice(2);
  141.  
  142.                     // Add module.
  143.                     targetTree.unshift(module);
  144.  
  145.                     return path.join(destination, targetTree.join('/'));
  146.                 }
  147.             },
  148.  
  149.             developmentViews:
  150.             {
  151.                 expand : true,
  152.                 cwd : 'src/',
  153.                 src : '*/views/**',
  154.                 dest : 'build/Development/views/',
  155.                 filter : 'isFile',
  156.                 rename : function(destination, source)
  157.                 {
  158.                     // Get the directory tree.
  159.                     var tree = source.split('/');
  160.  
  161.                     // Remove first two entries.
  162.                     var targetTree = tree.slice(2);
  163.  
  164.                     return path.join(destination, targetTree.join('/'));
  165.                 }
  166.             },
  167.  
  168.             releaseViews:
  169.             {
  170.                 expand : true,
  171.                 cwd : 'src/',
  172.                 src : '*/views/**',
  173.                 dest : 'build/Release/views/',
  174.                 filter : 'isFile',
  175.                 rename : function(destination, source)
  176.                 {
  177.                     // Get the directory tree.
  178.                     var tree = source.split('/');
  179.  
  180.                     // Remove first two entries.
  181.                     var targetTree = tree.slice(2);
  182.  
  183.                     return path.join(destination, targetTree.join('/'));
  184.                 }
  185.             }
  186.         },
  187.  
  188.         clean :
  189.         {
  190.             developmentBuild :
  191.             {
  192.                 src : 'build/Development'
  193.             },
  194.  
  195.             releaseBuild :
  196.             {
  197.                 src : 'build/Release'
  198.             }
  199.         }
  200.     };
  201.  
  202.     // Add compass, vsembed, vsrembed targets.
  203.     grunt.file.expand({filter : 'isFile'}, 'src/StoneOS.*/*.csproj').forEach(function (destination)
  204.     {
  205.         // Project file.
  206.         var projectName = path.basename(destination, '.csproj');
  207.  
  208.         var moduleName = getModuleName(projectName);
  209.  
  210.         // Extract directory.
  211.         var directory = path.dirname(destination);
  212.         directory = path.resolve(directory);
  213.  
  214.         // Generate assets path.
  215.         var assetsPath = path.join(directory, 'assets');
  216.         var assetsPathForSass = S(assetsPath).replaceAll('\\', '/');
  217.  
  218.         // Sass path.
  219.         var sassPath = path.join(assetsPath, 'sass');
  220.  
  221.         // Look up if Sass assets exists.
  222.         var assetsExists = fs.existsSync(sassPath);
  223.         var assetCount = assetsExists ? grunt.file.expand({ filter : 'isFile', cwd : sassPath }, '**/*.scss').length : 0;
  224.  
  225.         // grunt.log.writeln('Assets for', projectName, ':', assetCount);
  226.  
  227.         if (assetCount > 0)
  228.         {
  229.             var cssPath = path.join(assetsPath, 'styles');
  230.             var fontsPath = path.join(assetsPath, 'fonts');
  231.             var imagesPath = path.join(assetsPath, 'images');
  232.             var javascriptsPath = path.join(assetsPath, 'scripts');
  233.  
  234.             gruntConfig.compass[projectName] =
  235.             {
  236.                 options :
  237.                 {
  238.                     httpPath : '/assets/' + moduleName.toLowerCase() + '/',
  239.  
  240.                     sassDir : sassPath,
  241.  
  242.                     relativeAssets : true,
  243.                     noLineComments : true,
  244.  
  245.                     outputStyle : 'compressed',
  246.  
  247.                     cssDir : cssPath,
  248.                     fontsDir : fontsPath,
  249.                     imagesDir : imagesPath,
  250.                     javascriptsDir : javascriptsPath,
  251.  
  252.                     require : compassPlugins,
  253.  
  254.                     raw : [
  255.                         'cache_path = "'+ sassCachePathForSass +'"',
  256.                         'project_path = "'+ assetsPathForSass + '"',
  257.                         'sass_options = {:sourcemap => true}'
  258.                     ].join('\n') + '\n'
  259.                 }
  260.             };
  261.  
  262.             var embedRembedAssetTask =
  263.             {
  264.                 options:
  265.                 {
  266.                     project: directory,
  267.                     directories: ['assets'],
  268.                     extensions: assetExtensions
  269.                 }
  270.             };
  271.  
  272.             gruntConfig.vsembed[projectName + '.assets'] = embedRembedAssetTask;
  273.             gruntConfig.vsrembed[projectName + '.assets'] = embedRembedAssetTask;
  274.  
  275.             grunt.verbose.writeln('Registering \'asset\' embed/rembed task for', projectName);
  276.         }
  277.  
  278.         // Where views should be located.
  279.         var viewsPath = path.join(directory, 'views');
  280.  
  281.         // Look up if views exist.
  282.         var viewsExists = fs.existsSync(viewsPath);
  283.         var viewCount = viewsExists ? grunt.file.expand({ filter : 'isFile', cwd : viewsPath }, '**/*.cshtml').length : 0;
  284.  
  285.         // grunt.log.writeln('Views for', projectName, ':', viewCount);
  286.  
  287.         if (viewCount > 0)
  288.         {
  289.             var embedRembedViewsTask =
  290.             {
  291.                 options:
  292.                 {
  293.                     project: directory,
  294.                     directories: ['views'],
  295.                     extensions: '.cshtml'
  296.                 }
  297.             };
  298.  
  299.             gruntConfig.vsembed[projectName + '.views'] = embedRembedViewsTask;
  300.             gruntConfig.vsrembed[projectName + '.views'] = embedRembedViewsTask;
  301.  
  302.             grunt.verbose.writeln('Registering \'view\' embed/rembed task for', projectName);
  303.         }
  304.     });
  305.  
  306.     grunt.initConfig(gruntConfig);
  307.  
  308.     // Local tasks.
  309.     grunt.loadTasks('grunt_tasks');
  310.  
  311.     // NPM tasks.
  312.     grunt.loadNpmTasks('grunt-msbuild');
  313.     grunt.loadNpmTasks('grunt-contrib-compass');
  314.     grunt.loadNpmTasks('grunt-contrib-copy');
  315.     grunt.loadNpmTasks('grunt-contrib-clean');
  316.  
  317.     // Task groups.
  318.     grunt.registerTask('assets', ['compass']);
  319.     grunt.registerTask('embed', ['vsembed']);
  320.     grunt.registerTask('rembed', ['vsrembed']);
  321.  
  322.     grunt.registerTask('development', ['clean:developmentBuild', 'assets', 'copy:developmentAssets', 'copy:developmentViews', 'rembed', 'msbuild:development']);
  323.     grunt.registerTask('release', ['clean:releaseBuild', 'assets', 'embed', 'msbuild:release']);
  324.  
  325.     grunt.registerTask('default', function()
  326.     {
  327.         grunt.log.writeln('Nothing to do...');
  328.     });
  329. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement