1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Godbout\DashDocsetBuilder\Services; |
4
|
|
|
|
5
|
|
|
use Godbout\DashDocsetBuilder\Contracts\Docset; |
6
|
|
|
use Illuminate\Support\Facades\Artisan; |
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Illuminate\Support\Facades\File; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
|
12
|
|
|
final class DocsetPackager |
13
|
|
|
{ |
14
|
|
|
protected $docset; |
15
|
|
|
|
16
|
|
|
|
17
|
24 |
|
public function __construct(Docset $docset) |
18
|
|
|
{ |
19
|
24 |
|
$this->docset = $docset; |
20
|
24 |
|
} |
21
|
|
|
|
22
|
16 |
|
public function removePreviousDocsetFile() |
23
|
|
|
{ |
24
|
16 |
|
return Storage::deleteDirectory( |
25
|
16 |
|
$this->docset->file() |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
16 |
|
public function createDocsetFile() |
30
|
|
|
{ |
31
|
16 |
|
return Storage::makeDirectory( |
32
|
16 |
|
$this->docset->innerDirectory() |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
16 |
|
public function copyDocFiles() |
37
|
|
|
{ |
38
|
16 |
|
return File::copyDirectory( |
39
|
16 |
|
"storage/{$this->docset->downloadedDirectory()}", |
40
|
16 |
|
"storage/{$this->docset->innerDirectory()}" |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
16 |
|
public function createInfoPlist() |
45
|
|
|
{ |
46
|
8 |
|
$infoPlist = <<<EOT |
47
|
16 |
|
<?xml version="1.0" encoding="UTF-8"?> |
48
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
49
|
|
|
<plist version="1.0"> |
50
|
|
|
<dict> |
51
|
|
|
<key>CFBundleIdentifier</key> |
52
|
16 |
|
<string>{$this->docset->code()}</string> |
53
|
|
|
<key>CFBundleName</key> |
54
|
16 |
|
<string>{$this->docset->name()}</string> |
55
|
|
|
<key>DocSetPlatformFamily</key> |
56
|
16 |
|
<string>{$this->docset->code()}</string> |
57
|
|
|
<key>dashIndexFilePath</key> |
58
|
16 |
|
<string>{$this->docset->index()}</string> |
59
|
|
|
<key>DashDocSetFallbackURL</key> |
60
|
|
|
<string>https://</string> |
61
|
|
|
<key>DashDocSetPlayURL</key> |
62
|
16 |
|
<string>{$this->docset->playground()}</string> |
63
|
|
|
<key>isJavaScriptEnabled</key> |
64
|
|
|
<true/> |
65
|
|
|
<key>isDashDocset</key> |
66
|
|
|
<true/> |
67
|
|
|
<key>DashDocSetFamily</key> |
68
|
|
|
<string>dashtoc</string> |
69
|
|
|
<key>DashDocSetDeclaredInStyle</key> |
70
|
|
|
<string>originalName</string> |
71
|
|
|
</dict> |
72
|
|
|
</plist> |
73
|
|
|
EOT; |
74
|
|
|
|
75
|
16 |
|
return Storage::put( |
76
|
16 |
|
$this->docset->infoPlistFile(), |
77
|
|
|
$infoPlist |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
16 |
|
public function createAndPopulateSQLiteIndex() |
82
|
|
|
{ |
83
|
16 |
|
Config::set( |
84
|
16 |
|
'database.connections.sqlite.database', |
85
|
16 |
|
"storage/{$this->docset->databaseFile()}" |
86
|
|
|
); |
87
|
|
|
|
88
|
16 |
|
$this->createSQLiteIndex(); |
89
|
|
|
|
90
|
16 |
|
$this->populateSQLiteIndex(); |
91
|
16 |
|
} |
92
|
|
|
|
93
|
16 |
|
protected function createSQLiteIndex() |
94
|
|
|
{ |
95
|
16 |
|
Storage::put( |
96
|
16 |
|
$this->docset->databaseFile(), |
97
|
16 |
|
null |
98
|
|
|
); |
99
|
|
|
|
100
|
16 |
|
Artisan::call('migrate'); |
101
|
16 |
|
} |
102
|
|
|
|
103
|
16 |
|
protected function populateSQLiteIndex() |
104
|
|
|
{ |
105
|
16 |
|
$entries = $this->docsetEntries(); |
106
|
|
|
|
107
|
16 |
|
$entries->each(static function ($entry) { |
108
|
16 |
|
DB::table('searchIndex')->insert([ |
109
|
16 |
|
'name' => $entry['name'], |
110
|
16 |
|
'type' => $entry['type'], |
111
|
16 |
|
'path' => $entry['path'], |
112
|
|
|
]); |
113
|
16 |
|
}); |
114
|
16 |
|
} |
115
|
|
|
|
116
|
16 |
|
protected function docsetEntries() |
117
|
|
|
{ |
118
|
16 |
|
$files = $this->docset->htmlFiles(); |
119
|
|
|
|
120
|
16 |
|
$entries = collect(); |
121
|
|
|
|
122
|
16 |
|
$files->each(function ($file) use (&$entries) { |
123
|
8 |
|
$entries = $entries |
124
|
16 |
|
->merge($this->docset->entries($file)) |
125
|
16 |
|
->unique(function ($entry) { |
126
|
16 |
|
return $entry['name'] . $entry['type']; |
127
|
16 |
|
}); |
128
|
16 |
|
}); |
129
|
|
|
|
130
|
16 |
|
return $entries; |
131
|
|
|
} |
132
|
|
|
|
133
|
16 |
|
public function formatDocFiles() |
134
|
|
|
{ |
135
|
16 |
|
$files = $this->docset->htmlFiles(); |
136
|
|
|
|
137
|
16 |
|
$files->each(function ($file) { |
138
|
16 |
|
$formattedContent = $this->docset->format($file); |
139
|
16 |
|
Storage::put($file, $formattedContent); |
140
|
16 |
|
}); |
141
|
16 |
|
} |
142
|
|
|
|
143
|
16 |
|
public function copyIcons() |
144
|
|
|
{ |
145
|
16 |
|
if ($this->docset->icon16()) { |
146
|
16 |
|
Storage::copy( |
147
|
16 |
|
"{$this->docset->downloadedDirectory()}/{$this->docset->icon16()}", |
148
|
16 |
|
"{$this->docset->file()}/icon.png" |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
16 |
|
if ($this->docset->icon32()) { |
153
|
16 |
|
Storage::copy( |
154
|
16 |
|
"{$this->docset->downloadedDirectory()}/{$this->docset->icon32()}", |
155
|
16 |
|
"{$this->docset->file()}/[email protected]" |
156
|
|
|
); |
157
|
|
|
} |
158
|
16 |
|
} |
159
|
|
|
} |
160
|
|
|
|