Skip to content

Commit c3f8678

Browse files
author
Rafael Grigorian
committed
Version 1.0.0 Complete
1 parent c833081 commit c3f8678

File tree

31 files changed

+1261
-341
lines changed

31 files changed

+1261
-341
lines changed

dist/Varnish.zip

54.4 KB
Binary file not shown.

gruntfile.js

Lines changed: 166 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,188 @@ module.exports = function ( grunt ) {
44
var package = require ("./package.json");
55

66
// Load NPM tasks so we can use them in our configuration
7+
grunt.loadNpmTasks ("grunt-contrib-clean");
78
grunt.loadNpmTasks ("grunt-contrib-compress");
89
grunt.loadNpmTasks ("grunt-contrib-watch");
10+
grunt.loadNpmTasks ("grunt-git");
911
grunt.loadNpmTasks ("grunt-replace");
1012
grunt.loadNpmTasks ("grunt-mkdir");
1113
grunt.loadNpmTasks ("grunt-rsync");
1214

13-
grunt.task.registerTask ( "init", "initialize directories", function () {
14-
grunt.config.set ( "mkdir.execute.options.create", [ "dist" ] );
15-
grunt.task.run ( "mkdir:execute" );
16-
});
15+
grunt.task.registerTask ( "default", "preform a full build", [ "release" ]);
1716

18-
grunt.task.registerTask ( "deploy", "sync to production environment", function () {
19-
grunt.config.set ( "rsync", {
20-
options: {
21-
args: [ "--quiet" ],
22-
recursive: true
23-
},
24-
stage: {
25-
options: {
26-
src: "./src/app",
27-
dest: "/Users/raffi/Desktop/Magento-2-Staging/",
28-
delete: false
29-
}
17+
grunt.task.registerTask ( "deploy", "upload src contents to staging environment", function () {
18+
// Make sure that staging object is set in package JSON file
19+
if ( package.hasOwnProperty ( "staging" ) && package.staging.user.trim () != ""
20+
&& package.staging.host.trim () != "" && package.staging.port.trim () != ""
21+
&& package.staging.dest.trim () != "" ) {
22+
// Initialize the rsync option object
23+
var _rsync_options = {
24+
args: [ "-az", "-o", "-g", "-e 'ssh -p " + package.staging.port + "'" ],
25+
exclude: [],
26+
recursive: true
3027
}
31-
});
32-
grunt.task.run ( "rsync:stage" );
28+
// Save it into grunt config
29+
grunt.config.set ( "rsync.options", _rsync_options );
30+
// Traverse through jetrails dependencies
31+
package.jetrailsDependencies.forEach ( function ( dependency ) {
32+
// Extract the dependency name
33+
var name = dependency.match (/\/([a-zA-Z0-9-]*)\.git$/) [ 1 ].toLowerCase ();
34+
// Initialize the rsync dependencies options
35+
var _rsync_dependencies = {
36+
src: "lib/" + name + "/src/*",
37+
dest: package.staging.dest,
38+
host: package.staging.user + "@" + package.staging.host
39+
}
40+
// Configure the dependency rsync options
41+
grunt.config.set ( "rsync." + name + ".options", _rsync_dependencies );
42+
// Run the dependencies task
43+
grunt.task.run ( "rsync:" + name + "" );
44+
});
45+
// Initialize the rsync staging options
46+
var _rsync_staging = {
47+
src: "src/*",
48+
dest: package.staging.dest,
49+
host: package.staging.user + "@" + package.staging.host
50+
}
51+
// Save it into grunt config
52+
grunt.config.set ( "rsync.staging.options", _rsync_staging );
53+
// Run the task and upload the main source files
54+
grunt.task.run ( "rsync:staging" );
55+
}
56+
// Otherwise, report that we cannot deploy
57+
else {
58+
grunt.log.writeln ( "could not find staging details in package.json" );
59+
}
3360
});
3461

35-
grunt.task.registerTask ( "stream", "watch all files and deploy on change", function () {
36-
grunt.task.run ( "deploy" );
37-
grunt.config.set ( "watch", {
38-
src: {
39-
files: [ "./src/**/*" ],
40-
tasks: [ "deploy" ]
41-
}
42-
});
43-
grunt.task.run ( "watch:src" );
62+
grunt.task.registerTask ( "stream", "watch src and lib folders, deploy on change", function () {
63+
// Initialize the watch task options
64+
grunt.config.set ( "watch.module.files", [ "src/**/*", "lib/*/src/**/*" ] );
65+
grunt.config.set ( "watch.module.tasks", [ "deploy" ] );
66+
grunt.config.set ( "watch.module.options.spawn", false );
67+
// Run the task
68+
grunt.task.run ( "watch:module" );
4469
});
4570

46-
grunt.task.registerTask ( "version", "update version as defined in package.json", function () {
71+
grunt.task.registerTask ( "version", "update version, defined in package.json", function () {
72+
// Initialize the replacement patterns
73+
var _patterns = [
74+
{
75+
match: /^(\s*\*\s+@version\s+)([0-9]+\.[0-9]+\.[0-9]+)(\s*)$/gm,
76+
replacement: "$1" + package.version + "$3"
77+
},
78+
{
79+
match: /^(\s*<version>)([0-9]+\.[0-9]+\.[0-9]+)(<\/version>\s*)$/gm,
80+
replacement: "$1" + package.version + "$3"
81+
}
82+
];
83+
// Initialize which files are effected
84+
var _files = [
85+
{
86+
expand: true,
87+
flatten: false,
88+
src: [ "src/**/*.php", "src/**/*.phtml", "src/**/*.xml" ],
89+
dest: "."
90+
}
91+
];
92+
// Update the options in grunt config
93+
grunt.config.set ( "replace.version.options.patterns", _patterns );
94+
grunt.config.set ( "replace.version.files", _files );
95+
// Run the replace task
96+
grunt.task.run ( "replace:version" );
97+
});
4798

99+
grunt.task.registerTask ( "init", "initialize file structure", function () {
100+
// If no arguments are passed set the default config
101+
if ( arguments.length == 0 ) {
102+
grunt.config.set ( "mkdir.execute.options.create", [ "src", "dist", "lib" ] );
103+
grunt.task.run ( "mkdir:execute" );
104+
}
105+
// Check to see if there is one argument
106+
else if ( arguments.length == 1 ) {
107+
grunt.config.set ( "mkdir.execute.options.create", [ arguments [ 0 ] ] );
108+
grunt.task.run ( "mkdir:execute" );
109+
}
110+
// Warn user if there is more than one argument
111+
else {
112+
grunt.log.writeln ( this.name + ": No argument or one argument is valid" );
113+
}
48114
});
49115

50-
grunt.task.registerTask ( "package", "compress module into zip file", function () {
116+
grunt.task.registerTask ( "nuke", "clear generated file structure", function () {
117+
// If no arguments are passed set the default config
118+
if ( arguments.length == 0 ) {
119+
grunt.config.set ( "clean.execute", [ "dist", "lib" ] );
120+
grunt.task.run ( "clean:execute" );
121+
}
122+
// Check to see if there is one argument
123+
else if ( arguments.length == 1 ) {
124+
var paths = [ arguments [ 0 ] + "/**", arguments [ 0 ] + "/.*" ];
125+
grunt.config.set ( "clean.execute", paths );
126+
grunt.task.run ( "clean:execute" );
127+
}
128+
// Warn user if there is more than one argument
129+
else {
130+
grunt.log.writeln ( this.name + ": No argument or one argument is valid" );
131+
}
132+
});
51133

134+
grunt.task.registerTask ( "resolve", "downloads jetrails dependency extensions", function () {
135+
// Run the dependency command
136+
grunt.task.run ( "init" );
137+
// Loop through all the dependencies
138+
package.jetrailsDependencies.forEach ( function ( dependency ) {
139+
// Extract the dependency name
140+
var name = dependency.match (/\/([a-zA-Z0-9-]*)\.git$/) [ 1 ].toLowerCase ();
141+
// Check to see if we already cloned the repository
142+
if ( grunt.file.exists ( "lib", name ) ) {
143+
// Nuke it
144+
grunt.task.run ( "nuke:lib" );
145+
}
146+
// Set the configuration, and clone it into lib
147+
grunt.task.run ( "init:lib/" + name );
148+
grunt.config.set ( "gitclone." + name + ".options.repository", dependency );
149+
grunt.config.set ( "gitclone." + name + ".options.directory", "lib/" + name );
150+
grunt.task.run ( "gitclone:" + name );
151+
});
52152
});
53153

54-
grunt.task.registerTask ( "default", "preform a full build", [ "init", "version", "deploy", "package" ] );
154+
grunt.task.registerTask ( "release", "prepares file structure for github release", function () {
155+
// Run the dependency tasks
156+
grunt.task.run ( "init" );
157+
grunt.task.run ( "resolve" );
158+
// Change version numbers
159+
grunt.task.run ( "version" );
160+
// Clear dist folder
161+
grunt.task.run ( "nuke:dist" );
162+
// Initialize the files array for compression
163+
var files = [{
164+
cwd: "src",
165+
expand: true,
166+
src: ["**"]
167+
}];
168+
// Traverse through jetrails dependencies
169+
package.jetrailsDependencies.forEach ( function ( dependency ) {
170+
// Extract the dependency name
171+
var name = dependency.match (/\/([a-zA-Z0-9-]*)\.git$/) [ 1 ].toLowerCase ();
172+
// Append to the files array
173+
files.push ({
174+
cwd: "lib/" + name + "/src",
175+
expand: true,
176+
src: ["**"]
177+
});
178+
});
179+
// Define the output file
180+
var _output = package.name.replace ( "-", "_" ) + ".zip";
181+
// Define other options
182+
grunt.config.set ( "compress.module.options.archive", "dist/" + _output );
183+
grunt.config.set ( "compress.module.options.mode", "zip" );
184+
grunt.config.set ( "compress.module.files", files );
185+
// Compress the module
186+
grunt.task.run ( "compress:module" );
187+
// Nuke the lib folder
188+
grunt.task.run ( "nuke:lib" );
189+
});
55190

56-
}
191+
};

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
{
2-
"name": "varnish",
2+
"name": "Varnish",
33
"version": "1.0.0",
44
"description": "Magento 2 - Manage Varnish Cache",
55
"repository": {
66
"type": "git",
77
"url": "git+https://github.com/jetrails/varnish.git"
88
},
9+
"company": "JetRails®",
910
"author": "Rafael Grigorian",
1011
"license": "UNLICENSED",
12+
"copyright": "JetRails®, all rights reserved",
1113
"bugs": {
1214
"url": "https://github.com/jetrails/varnish/issues"
1315
},
1416
"homepage": "https://github.com/jetrails/varnish#readme",
15-
"dependencies": {},
1617
"devDependencies": {
1718
"grunt": "^1.0.1",
19+
"grunt-contrib-clean": "^1.1.0",
1820
"grunt-contrib-compress": "^1.4.3",
1921
"grunt-contrib-watch": "^1.0.0",
2022
"grunt-mkdir": "^1.0.0",
2123
"grunt-replace": "^1.0.1",
2224
"grunt-rsync": "^2.0.1"
25+
},
26+
"jetrailsDependencies": [],
27+
"staging": {
28+
"port": "22",
29+
"user": "raffi",
30+
"host": "localhost",
31+
"dest": "~/Site/"
2332
}
2433
}

src/app/code/JetRails/Varnish/Block/Adminhtml/System/Config/Form/Export.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,60 @@
22

33
namespace JetRails\Varnish\Block\Adminhtml\System\Config\Form;
44

5-
use Magento\Backend\Block\Template\Context;
65
use Magento\Config\Block\System\Config\Form\Field;
76
use Magento\Framework\Data\Form\Element\AbstractElement;
87

8+
/**
9+
* Export.php - This block is used to generate the HTML button in the store configuration
10+
* section of magento. The button is described as a frontend model in the system.xml file.
11+
* @version 1.0.0
12+
* @package JetRails® Varnish
13+
* @category Form
14+
* @author Rafael Grigorian - JetRails®
15+
* @copyright JetRails®, all rights reserved
16+
*/
917
class Export extends Field {
1018

19+
/**
20+
* This variable contains the path to the template file for the export button.
21+
* @var String _template Path to template file for button
22+
*/
1123
protected $_template = "JetRails_Varnish::adminhtml/system/config/form/export.phtml";
1224

13-
public function __construct ( Context $context, array $data = [] ) {
14-
parent::__construct ( $context, $data );
15-
}
16-
25+
/**
26+
* This method basically redirects the output of the _toHtml method in the parent class to
27+
* the output of this method.
28+
* @param AbstractElement element This is ignored and unused
29+
* @return String Button HTML
30+
*/
1731
protected function _getElementHtml ( AbstractElement $element ) {
32+
// Return the result of the _toHtml method in parent class
1833
return $this->_toHtml ();
1934
}
2035

21-
public function render ( AbstractElement $element ) {
22-
$element->unsScope ()->unsCanUseWebsiteValue ()->unsCanUseDefaultValue ();
23-
return parent::render ( $element );
24-
}
25-
36+
/**
37+
* This method is used within the template, and it returns the url for the action event.
38+
* @return String Controller URL for action event
39+
*/
2640
public function getActionUrl () {
41+
// Construct the url form the controller path
2742
return $this->getUrl ("varnish/export/config");
2843
}
2944

45+
/**
46+
* This method is used within the template file and it simply constructs a button widget and
47+
* set's the button's properties.
48+
* @return String The button HTML with properties set
49+
*/
3050
public function getButtonHtml () {
51+
// Create a button widget and set it's properties
3152
$button = $this->getLayout ()
3253
->createBlock ("Magento\Backend\Block\Widget\Button")
3354
->setData ([
34-
"label" => "Export VCL for Varnish 4",
35-
'onclick' => "setLocation('{$this->getUrl('varnish/export/config')}')"
55+
"label" => "Export VCL for Varnish 4",
56+
"onclick" => "setLocation('{$this->getUrl('varnish/export/config')}')"
3657
]);
58+
// Return the constructed button's HTML
3759
return $button->toHtml ();
3860
}
3961

src/app/code/JetRails/Varnish/Block/Adminhtml/System/Config/Form/Link.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,44 @@
22

33
namespace JetRails\Varnish\Block\Adminhtml\System\Config\Form;
44

5-
use Magento\Backend\Block\Template\Context;
65
use Magento\Config\Block\System\Config\Form\Field;
76
use Magento\Framework\Data\Form\Element\AbstractElement;
87

8+
/**
9+
* Link.php - This block alongside the template is used to render a link to the cache management
10+
* page in the magento store backend. This block is also referenced as a frontend model in the
11+
* system.xml file.
12+
* @version 1.0.0
13+
* @package JetRails® Varnish
14+
* @category Form
15+
* @author Rafael Grigorian - JetRails®
16+
* @copyright JetRails®, all rights reserved
17+
*/
918
class Link extends Field {
1019

20+
/**
21+
* This variable contains the path to the template file for the export button.
22+
* @var String _template Path to template file for button
23+
*/
1124
protected $_template = "JetRails_Varnish::adminhtml/system/config/form/link.phtml";
1225

26+
/**
27+
* This method basically redirects the output of the _toHtml method in the parent class to
28+
* the output of this method.
29+
* @param AbstractElement element This is ignored and unused
30+
* @return String Button HTML
31+
*/
1332
protected function _getElementHtml ( AbstractElement $element ) {
33+
// Return the result of the _toHtml method in parent class
1434
return $this->_toHtml ();
1535
}
1636

37+
/**
38+
* This method is used within the template, and it returns the url for the action event.
39+
* @return String Controller URL for action
40+
*/
1741
public function getActionUrl () {
42+
// Construct the url form the controller path
1843
return $this->getUrl ("adminhtml/cache/index");
1944
}
2045

0 commit comments

Comments
 (0)