Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
 
App::import(array('HttpSocket', 'File', 'Xml'));
/**
 * Plugin management shell.
 *
 * @package default
 */
class PlugShell extends Shell {
 
    /**
     * HttpSocket instance.
     * @var string
     */
    var $Socket = null;
 
    /**
     * Plugins server hostname.
     * @var string
     */
    var $serverUri = 'http://plugins.thoughtglade.com';
 
    /**
     * Main shell logic.
     *
     * @return void
     * @author John David Anderson
     */
    function main() {
 
        if(isset($this->params['server'])) {
            $this->serverUri = $this->params['server'];
            $this->out('Using specified server: ' . $this->serverUri);
        }
 
        $this->Socket = new HttpSocket();
 
        if(!empty($this->command)) {
            $this->command = substr($this->command, 0, 1);
        }
 
        $this->__run();
    }
 
    /**
     * Main application flow control.
     *
     * @return void
     * @author John David Anderson
     */
    function __run() {
        $validCommands = array('l', 'v', 'i', 'p', 'q');
 
        while (empty($this->command)) {
            $this->out("Github Plugin Server");
            $this->out("---------------------------------------------------------------");
            $this->out("[L]ist Installed Plugins");
            $this->out("[V]iew Available Plugins");
            $this->out("[I]nstall Plugin as Submodule or from Zip");
            $this->out("[P]ull all Plugin Submodule Updates");
            $this->out("[Q]uit");
            $temp = $this->in("What command would you like to perform?", $validCommands, 'i');
            if (in_array(strtolower($temp), $validCommands)) {
                $this->command = $temp;
            } else {
                $this->out("Try again.");
            }
        }
 
        switch ($this->command) {
            case 'l' :
                $this->__doList();
                break;
            case 'v' :
                $this->__doView();
                break;
            case 'i' :
                $this->__doInstall();
                break;
            case 'p' :
                $this->__doPull();
                break;
            case 'q' :
                $this->out(__("Exit", true));
                $this->_stop();
                break;
        }
    }
 
    function __doList() {
        $this->out("\nThis is a list of currently installed plugins in your application.");
        foreach ($this->__listPlugins() as $key => $plugin) {
            $this->out(" " . $key+1 . ". " . Inflector::humanize($plugin) . " Plugin");
        }
    }
 
    function __doView() {
        $this->out("\nThis is a list of currently active plugins on the server.");
        foreach ($this->__listServerPlugins() as $key => $plugin) {
            $this->out($key+1 . ". " . $plugin['name'] . " Plugin");
        }
    }
   
    function __doInstall() {
        $validCommands = array();
        $availablePlugins = $this->__listServerPlugins();
       
        foreach ($availablePlugins as $key => $plugin) {
            $this->out($key+1 . ". " . Inflector::humanize($plugin['name']) . " Plugin");
            $validCommands[] = $key+1;
        }
       
        $validCommands[] = 'q';
        $enteredPlugin = null;
       
        while ($enteredPlugin === null) {
            $enteredPlugin = $this->in(__("Enter a number from the list above  or 'q' or nothing to exit", true), null, 'q');
            if ($enteredPlugin === 'q') {
                $this->out(__("Exit", true));
                $this->_stop();
            } elseif (in_array($enteredPlugin, $validCommands)) {
                // So now we actually have to install this plugin...
                // Lets construct the repoURL
                $repoURL = "git://github.com/" . $availablePlugins[$enteredPlugin-1]['owner'] . "/" . $availablePlugins[$enteredPlugin-1]['name'] . ".git";
                // Get the name under which the user would like to place this plugin
                $pluginName = $this->in(__("Enter a name for this plugin or 'q' to exit", true), null, 'q');
                if ($pluginName === 'q') {
                    $this->out(__("Exit", true));
                    $this->_stop();
                } else {
                    $topLevelDirectory = $this->in(__("If you aren't running this from the toplevel of the working tree, specify the full toplevel path. Press enter if this is the toplevel."), null, '');
                    if ($topLevelDirectory !== null) {
                        $pluginRelativePath = $this->in(__("What is the relative path of the plugin folder? Include the directory of the plugins folder, and leave off the beginning and trailing slash."), null, '');
                        if ($pluginRelativePath !== null) {
                            $this->out("Adding Submodule...");
                            $this->out(shell_exec("cd {$topLevelDirectory} ; git submodule add {$repoURL} {$pluginRelativePath}/{$pluginName}"));
                            $this->out("Initializing Submodule...");
                            $this->out(shell_exec("cd {$topLevelDirectory} ; git submodule init"));
                            $this->out("Updating Submodule...");
                            $this->out(shell_exec("cd {$topLevelDirectory} ; git submodule update"));
                        }
                    } else {
                        $this->out("Adding Submodule...");
                        $this->out(shell_exec("git submodule add {$repoURL} plugins/{$pluginName}"));
                        $this->out("Initializing Submodule...");
                        $this->out(shell_exec("git submodule init"));
                        $this->out("Updating Submodule...");
                        $this->out(shell_exec("git submodule update"));
                    }
                }
            } else {
                $enteredPlugin = null;
            }
        }
    }
 
    /**
     * Loads a list of plugins in the current app.
     *
     * @return void
     * @author John David Anderson
     */
    function __listPlugins() {
        $pluginsFolder = new Folder(APP . 'plugins');
        $filesAndDirectories = $pluginsFolder->ls(true, true);
        return $filesAndDirectories[0];
    }
   
    function __listServerPlugins() {
        $githubServer = "http://github.com/api/v2/xml/";
        $githubUser = 'josegonzalez';
        $xmlResponse = new Xml(
            $this->Socket->get(
                $githubServer . 'repos/show/' . $githubUser));
        $arrayResponse = Set::reverse($xmlResponse);
        return $arrayResponse['Repositories']['Repository'];
    }
}
?>