forked from pattern-lab/patternlab-php-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMigrator.php
More file actions
161 lines (116 loc) · 4.49 KB
/
Migrator.php
File metadata and controls
161 lines (116 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*!
* Migrator Class
*
* Copyright (c) 2014 Dave Olsen http://dmolsen.com
* Licensed under the MIT license
*
* Moves any necessary files from core/ into source/ or public/
*
* THIS CLASS ISN'T USED ANYMORE. KEEPING IT AROUND JUST IN CASE.
*
* Example migration.json:
* {
* "sourcePath": "core/styleguide",
* "destinationPath": "public/styleguide",
* "checkType": "versionDiffDir"
* }
*
*/
namespace PatternLab;
use \PatternLab\Timer;
class Migrator {
/**
* Make sure the config paths are set
*/
public function __construct() {
// don't do anything
}
/**
* Read through the migrations and move files as needed
* @param {Boolean} is this a different version
*/
public function migrate($diffVersion = false) {
$migrations = new \DirectoryIterator(__DIR__."/../../migrations/");
$migrationsValid = array();
foreach ($migrations as $migration) {
$filename = $migration->getFilename();
if (!$migration->isDot() && $migration->isFile() && ($filename[0] != "_")) {
$migrationsValid[] = $filename;
}
}
asort($migrationsValid);
foreach ($migrationsValid as $filename) {
$basePath = __DIR__."/../../../";
$migrationData = json_decode(file_get_contents(__DIR__."/../../migrations/".$filename));
$checkType = $migrationData->checkType;
$sourcePath = ($checkType == "fileExists") ? $basePath.$migrationData->sourcePath : $basePath.$migrationData->sourcePath.DIRECTORY_SEPARATOR;
$destinationPath = ($checkType == "fileExists") ? $basePath.$migrationData->destinationPath : $basePath.$migrationData->destinationPath.DIRECTORY_SEPARATOR;
if ($checkType == "dirEmpty") {
$emptyDir = true;
$objects = new \DirectoryIterator($destinationPath);
foreach ($objects as $object) {
if (!$object->isDot() && ($object->getFilename() != "README") && ($object->getFilename() != ".DS_Store")) {
$emptyDir = false;
}
}
if ($emptyDir) {
$this->runMigration($filename, $sourcePath, $destinationPath, false);
}
} else if ($checkType == "dirExists") {
if (!is_dir($destinationPath)) {
mkdir($destinationPath);
}
} else if ($checkType == "fileExists") {
if (!file_exists($destinationPath)) {
$this->runMigration($filename, $sourcePath, $destinationPath, true);
}
} else if (($checkType == "versionDiffDir") && $diffVersion) {
// make sure the destination path exists
if (!is_dir($destinationPath)) {
mkdir($destinationPath);
}
$this->runMigration($filename, $sourcePath, $destinationPath, false);
} else if (($checkType == "versionDiffFile") && $diffVersion) {
$this->runMigration($filename, $sourcePath, $destinationPath, true);
} else {
print "pattern Lab doesn't recognize a checkType of ".$checkType.". The migrator class is pretty thin at the moment.\n";
exit;
}
}
}
/**
* Run any migrations found in core/migrations that match the approved types
* @param {String} the filename of the migration
* @param {String} the path of the source directory
* @param {String} the path to the destination
* @param {Boolean} moving a single file or a directory
*/
protected function runMigration($filename, $sourcePath, $destinationPath, $singleFile) {
$filename = str_replace(".json","",$filename);
print "starting the ".$filename." migration...\n";
if ($singleFile) {
copy($sourcePath.$fileName,$destinationPath.$fileName);
} else {
if (strpos($sourcePath,"pattern-lab/") !== false) {
$sourcePath = str_replace(__DIR__."/../../../","",rtrim($sourcePath,"/"));
$f = new Fetch();
$f->fetch("starterkit",$sourcePath);
} else {
$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($sourcePath), \RecursiveIteratorIterator::SELF_FIRST);
$objects->setFlags(\FilesystemIterator::SKIP_DOTS);
foreach ($objects as $object) {
// clean-up the file name and make sure it's not one of the pattern lab files or to be ignored
$fileName = str_replace($sourcePath,"",$object->getPathname());
// check to see if it's a new directory
if ($object->isDir() && !is_dir($destinationPath.$fileName)) {
mkdir($destinationPath.$fileName);
} else if ($object->isFile()) {
copy($sourcePath.$fileName,$destinationPath.$fileName);
}
}
}
}
print "completed the ".$filename." migration...\n";
}
}