Method: PluginTool.make_new_plugin

Defined in:
lib/new_plugin.rb

.make_new_plugin(plugin_name) ⇒ Object



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
# File 'lib/new_plugin.rb', line 4

def self.make_new_plugin(plugin_name)
  unless plugin_name =~ /\A[a-z0-9_]+\z/ && plugin_name.length > 8
    end_on_error "Bad plugin name - must use a-z0-9_ only, and more than 8 characters."
  end
  if File.exist?(plugin_name)
    end_on_error "File or directory #{plugin_name} already exists"
  end
  FileUtils.mkdir(plugin_name)
  ['js', 'static', 'template', 'test'].each do |dir|
    FileUtils.mkdir("#{plugin_name}/#{dir}")
  end
  random = java.security.SecureRandom.new()
  rbytes = Java::byte[20].new
  random.nextBytes(rbytes)
  install_secret = String.from_java_bytes(rbytes).unpack('H*').join
  plugin_url_fragment = plugin_name.gsub('_','-')
  File.open("#{plugin_name}/plugin.json",'w') do |file|
    file.write(<<__E)
{
"pluginName": "#{plugin_name}",
"pluginAuthor": "TODO Your Company",
"pluginVersion": 1,
"displayName": "#{plugin_name.split('_').map {|e| e.capitalize} .join(' ')}",
"displayDescription": "TODO Longer description of plugin",
"installSecret": "#{install_secret}",
"apiVersion": 4,
"load": [
    "js/#{plugin_name}.js"
  ],
"respond": ["/do/#{plugin_url_fragment}"]
}
__E
  end
  File.open("#{plugin_name}/js/#{plugin_name}.js",'w') do |file|
    file.write(<<__E)

P.respond("GET", "/do/#{plugin_url_fragment}/example", [
], function(E) {
  E.render({
      pageTitle: "Example page"
  });
});

__E
  end
  File.open("#{plugin_name}/template/example.html",'w') do |file|
    file.write(<<__E)
<p>This is an example template.</p>
__E
  end
  File.open("#{plugin_name}/test/#{plugin_name}_test1.js",'w') do |file|
    file.write(<<__E)

t.test(function() {

  // For documentation, see
  //   http://docs.oneis.co.uk/dev/plugin/tests

  t.assert(true);

});

__E
  end
  File.open("#{plugin_name}/requirements.schema",'w') do |file|
    file.write("\n\n\n")
  end
  puts <<__E

Plugin #{plugin_name} has been created. Run

oneis-plugin -p #{plugin_name}

to upload it to the server, then visit

https://<HOSTNAME>/do/#{plugin_url_fragment}/example

to see a sample page.

See http://docs.oneis.co.uk/dev/plugin for more information.

__E
end