From 5c76ada4a409f2a9948b3ac3efd9899cff36c1b6 Mon Sep 17 00:00:00 2001
From: Volodymyr Zaets <vzaets@magento.com>
Date: Tue, 23 Aug 2016 13:11:27 +0300
Subject: [PATCH] MAGETWO-55345: Grunt uses actual theme's list

---
 Gruntfile.js.sample                   |  3 +-
 dev/tools/grunt/configs/clean.js      |  2 +-
 dev/tools/grunt/configs/combo.js      |  2 +-
 dev/tools/grunt/configs/exec.js       |  2 +-
 dev/tools/grunt/configs/less.js       |  2 +-
 dev/tools/grunt/configs/watch.js      |  2 +-
 dev/tools/grunt/tools/files-router.js | 60 +++++++++++++++++++++++++++
 grunt-config.json.sample              |  3 ++
 8 files changed, 70 insertions(+), 6 deletions(-)
 create mode 100644 dev/tools/grunt/tools/files-router.js
 create mode 100644 grunt-config.json.sample

diff --git a/Gruntfile.js.sample b/Gruntfile.js.sample
index 44c5e897dd3..ec0cd43ec3c 100644
--- a/Gruntfile.js.sample
+++ b/Gruntfile.js.sample
@@ -5,12 +5,13 @@
 
 // For performance use one level down: 'name/{,*/}*.js'
 // If you want to recursively match all subfolders, use: 'name/**/*.js'
+
 module.exports = function (grunt) {
     'use strict';
 
     var _ = require('underscore'),
         path = require('path'),
-        themes = require('./dev/tools/grunt/configs/themes'),
+        themes = require('./dev/tools/grunt/tools/files-router').getThemes(),
         configDir = './dev/tools/grunt/configs',
         tasks = grunt.file.expand('./dev/tools/grunt/tasks/*');
 
diff --git a/dev/tools/grunt/configs/clean.js b/dev/tools/grunt/configs/clean.js
index 792981a632c..d984620feb6 100644
--- a/dev/tools/grunt/configs/clean.js
+++ b/dev/tools/grunt/configs/clean.js
@@ -5,7 +5,7 @@
 
 'use strict';
 
-var themes = require('./themes'),
+var themes = require('../tools/files-router').getThemes(),
     _      = require('underscore');
 
 var themeOptions = {};
diff --git a/dev/tools/grunt/configs/combo.js b/dev/tools/grunt/configs/combo.js
index 930b2fd6871..7dca3a268db 100644
--- a/dev/tools/grunt/configs/combo.js
+++ b/dev/tools/grunt/configs/combo.js
@@ -5,7 +5,7 @@
 
 'use strict';
 
-var theme = require('./themes'),
+var theme = require('../tools/files-router').getThemes(),
     path = require('./path');
 
 /**
diff --git a/dev/tools/grunt/configs/exec.js b/dev/tools/grunt/configs/exec.js
index a26a0d95a54..ee06b371347 100644
--- a/dev/tools/grunt/configs/exec.js
+++ b/dev/tools/grunt/configs/exec.js
@@ -6,7 +6,7 @@
 'use strict';
 
 var combo = require('./combo'),
-    themes = require('./themes'),
+    themes = require('../tools/files-router').getThemes(),
     _      = require('underscore');
 
 var themeOptions = {};
diff --git a/dev/tools/grunt/configs/less.js b/dev/tools/grunt/configs/less.js
index 4071b47fa2b..6f3b0f7ae6d 100644
--- a/dev/tools/grunt/configs/less.js
+++ b/dev/tools/grunt/configs/less.js
@@ -6,7 +6,7 @@
 'use strict';
 
 var combo  = require('./combo'),
-    themes = require('./themes'),
+    themes = require('../tools/files-router').getThemes(),
     _      = require('underscore');
 
 var themeOptions = {};
diff --git a/dev/tools/grunt/configs/watch.js b/dev/tools/grunt/configs/watch.js
index 014b6b065a9..880b7f7e392 100644
--- a/dev/tools/grunt/configs/watch.js
+++ b/dev/tools/grunt/configs/watch.js
@@ -6,7 +6,7 @@
 'use strict';
 
 var combo  = require('./combo'),
-    themes = require('./themes'),
+    themes = require('../tools/files-router').getThemes(),
     _      = require('underscore');
 
 var themeOptions = {};
diff --git a/dev/tools/grunt/tools/files-router.js b/dev/tools/grunt/tools/files-router.js
new file mode 100644
index 00000000000..dad794004ec
--- /dev/null
+++ b/dev/tools/grunt/tools/files-router.js
@@ -0,0 +1,60 @@
+/**
+ * Copyright © 2016 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+'use strict';
+
+module.exports = {
+    defaultConfig: {
+        'themes': 'dev/tools/grunt/configs/themes'
+    },
+
+    /**
+     * Immediately invoked function.
+     * Loads user config file.
+     */
+    userConfig: (function () {
+        try {
+            return require(process.cwd() + '/grunt-config');
+        } catch (error) {
+            return null;
+        }
+    })(),
+
+    /**
+     * Loads "themes" file.
+     * Load priority:
+     *      From user config;
+     *      From default config with ".loc" suffix ;
+     *      From default config;
+     *
+     * @returns themes file or error
+     */
+    getThemes: function () {
+        if (this.userConfig && this.userConfig.themes) {
+            return require(this.getFullPath(this.userConfig.themes));
+        } else {
+            try {
+                return require(this.getFullPath(this.defaultConfig.themes + '.loc'));
+            } catch (error) {
+                try {
+                    return require(this.getFullPath(this.defaultConfig.themes));
+                } catch (error) {
+                    throw  error;
+                }
+            }
+        }
+    },
+
+    /**
+     * Generates full path to file.
+     *
+     * @param {String} path - relative path to file.
+     *
+     * @returns {String} Full path to file
+     */
+    getFullPath: function (path) {
+        return process.cwd() + '/' + path;
+    }
+};
diff --git a/grunt-config.json.sample b/grunt-config.json.sample
new file mode 100644
index 00000000000..7ef28a856f9
--- /dev/null
+++ b/grunt-config.json.sample
@@ -0,0 +1,3 @@
+{
+    "themes": "dev/tools/grunt/configs/local-themes"
+}
-- 
GitLab