Add check gem installed.

Add bundle install after generating plugin.
This commit is contained in:
BoHung Chiu 2021-03-04 14:33:01 +08:00
parent 998ffa0a8a
commit 256fd5784b
9 changed files with 174 additions and 8 deletions

View File

@ -28,7 +28,7 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
end
def create
personal_plugin_field = PersonalPluginField.create(personal_plugin_field_params)
redirect_to params[:referer_url]
redirect_to admin_personal_plugin_fields_path
end
def edit
@ -42,7 +42,7 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
def update
@personal_plugin_field.update_attributes(personal_plugin_field_params)
@personal_plugin_field.save
redirect_to params[:referer_url]
redirect_to admin_personal_plugin_fields_path
end
def fields_setting
end
@ -131,6 +131,24 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
"<%= #{plugin_template}.#{field_name} %>"
end
end
backend_profile_fields = @personal_plugin_field.backend_fields["profile"] rescue []
backend_profile_fields_contents = backend_profile_fields.map do |field_name|
if field_name == slug_title
"\r\n <%= link_to #{plugin_template}.#{field_name}, page_for_#{plugin_template}(#{plugin_template}), target: \"blank\" %>\r\n" +
" <div class=\"quick-edit\">\r\n"+
" <ul class=\"nav nav-pills hide\">\r\n"+
" <li><%= link_to t('edit'), edit_admin_#{plugin_template}_path(#{plugin_template},:page => params[:page]) %></li>\r\n"+
" <li><%= link_to t(:delete_), admin_#{plugin_template}_path(id: #{plugin_template}.id, :page => params[:page]), method: :delete, data: { confirm: 'Are you sure?' } %></li>\r\n"+
" </ul>\r\n"+
" </div>\r\n "
elsif field_name.include?(".")
"<%= #{plugin_template}.#{field_name} rescue \"\" %>"
elsif fields.include?(field_name) || plugin_template_related_members.include?(field_name) || field_name == "member_profile" #file or link or member
"<%= #{plugin_template}.display_field(\"#{field_name}\").html_safe rescue \"\" %>"
else
"<%= #{plugin_template}.#{field_name} %>"
end
end
col_name_to_show = @personal_plugin_field.frontend_fields["member_show"] rescue []
col_name_to_show_in_show_page = @personal_plugin_field.frontend_fields["show"] rescue []
col_name_to_show_in_index_page = @personal_plugin_field.frontend_fields["index"] rescue []
@ -160,8 +178,6 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
related_locale_fields_input_fields << f3
related_none_locale_fields_input_fields << f4
end
backend_profile_fields = backend_index_fields
backend_profile_fields_contents = backend_index_fields_contents
datetime_field_types_hash = {"year_month"=>"%Y/%m","date"=>"%Y/%m/%d","time"=>"%H:%M","date_time"=>"%Y/%m/%d %H:%M"}
datetime_fields = primary_modal_fields.select{|field_value| datetime_field_types_hash.keys.include?(field_value[:field_type])}.map{|field_value|
[field_value[:field_name],datetime_field_types_hash[field_value[:field_type]]]
@ -293,6 +309,8 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
copy_templates("#{cp_template_dir_path}modules/#{@personal_plugin_field.module_name}/")
#render :html => @logs#@match_pattern#@logs.join("<br>").html_safe#@match_pattern
@personal_plugin_field.update(:log_text=>"")
add_plugin("downloaded_extensions.rb",@personal_plugin_field.module_name)
bundle_install
render :json => {:success=>true,:url=>"/admin/#{plugin_template.pluralize}/",:name=>@personal_plugin_field.title}
rescue => e
@match_pattern = {"parse_again_start"=>"","parse_again_end"=>"","col_name_translate_yaml"=>""}
@ -549,6 +567,55 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
can_install = !gem_plugins.include?(params[:plugin_name])
can_install = (PersonalPluginField.find(params[:id]).module_name == params[:plugin_name] rescue false) if !can_install
render :json => {:can_install => can_install }
end
def add_plugin(extention_file,plugin_name)
txt = File.read(extention_file) rescue nil
if txt.nil?
File.open(extention_file,'w+'){|f| f.write("")}
txt = ""
end
txt_scan = txt.scan(/^[\n]*gem\s*["']#{plugin_name}["'].*\n/)
if txt_scan.count != 1
delete_plugin(extention_file,plugin_name)
txt = File.read(extention_file) rescue ""
txt = txt + "\r\ngem \"#{plugin_name}\", path: \"#{Rails.root}/tmp/#{plugin_name}\"\r\n"
File.open(extention_file,'w+') do |f|
f.write(txt)
end
end
end
def delete_plugin(extention_file,plugin_name)
txt = File.read(extention_file) rescue nil
return if txt.nil?
txt_change = txt.gsub(/^[\n]*gem\s*["']#{plugin_name}["'].*\n/,'')
if txt_change != txt
File.open(extention_file,'w+') do |f|
f.write(txt_change)
end
end
end
def check_modal_name
primary_modal_names = PersonalPluginField.where(:id.ne=>params[:id]).pluck(:primary_modal_name)
related_modal_names = PersonalPluginField.where(:id.ne=>params[:id]).pluck(:related_modal_name).flatten.uniq
other_modal_names = primary_modal_names + related_modal_names
personal_plugin_field = PersonalPluginField.where(:id=>params[:id]).first
all_modal_names = PersonalPluginField.get_modal_names_cache
if personal_plugin_field.present?
except_modals = Dir.glob("tmp/#{personal_plugin_field.module_name}/app/models/*.rb").map{|f|
fn = File.basename(f)
fn.slice(0,fn.length - 3)
}
all_modal_names = all_modal_names - except_modals
end
all_modal_names = all_modal_names + other_modal_names
all_modal_names = all_modal_names.uniq
self_modal_names = params[:modal_names]
invalid_modal_names = self_modal_names.select{|n| all_modal_names.include?(n)}
if invalid_modal_names.count != 0
render :json => {:success=>false,:invalid_modal_names=>invalid_modal_names}
else
render :json => {:success=>true}
end
end
private
def personal_plugin_field_params
@ -571,6 +638,7 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
def set_personal_plugin_field
PersonalPluginField.get_modal_names_cache
path = request.path.split('/')
if path.last.include? '-'
uid = path[-1].split("-").last
@ -581,4 +649,9 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController
end
@personal_plugin_field = PersonalPluginField.find_by(:uid => uid) rescue PersonalPluginField.find(params[:id] || params[:personal_plugin_field_id])
end
def bundle_install
Bundler.with_clean_env { system("cd #{Rails.root} && bundle install") }
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 2
end
end

View File

@ -14,7 +14,22 @@ class PersonalPluginField
field :frontend_fields, :type => Hash, :default => {}
field :log_text, :type => String, :default => ""
field :fields_order, :type => Hash, :default => {}
before_save :check_plugin_exist
field :copy_id
before_save :change_extensions,:check_modal_name
after_destroy do
delete_plugin("downloaded_extensions.rb",self.module_name)
restart_server
end
before_create do
if self.copy_id
copy_item = self.class.find(copy_id) rescue nil
if !copy_item.nil?
self.backend_fields = copy_item.backend_fields
self.frontend_fields = copy_item.frontend_fields
self.fields_order = copy_item.fields_order
end
end
end
def get_all_gem_plugins
extention_files = ["downloaded_extensions.rb","built_in_extensions.rb"]
gem_plugins = extention_files.map{|f| (File.read(f).scan(/\n\s*gem\s*["'](.*)["']\s*,/).flatten rescue [])}.flatten
@ -25,4 +40,57 @@ class PersonalPluginField
can_install = (self.class.where(:module_name=>self.module_name,:id.ne=>self.id).count == 0 rescue false) if !can_install
return can_install
end
def change_extensions
if !check_plugin_exist
return false
else
extention_file = "downloaded_extensions.rb"
if self.module_name_changed?
if self.module_name_was.present?
delete_plugin(extention_file,module_name_was)
end
end
return true
end
end
def delete_plugin(extention_file,plugin_name)
txt = File.read(extention_file) rescue nil
return if txt.nil?
txt_change = txt.gsub(/^[\n]*gem\s*["']#{plugin_name}["'].*\n/,'')
if txt_change != txt
File.open(extention_file,'w+') do |f|
f.write(txt_change)
end
end
end
def check_modal_name
primary_modal_names = self.class.where(:id.ne=>self.id).pluck(:primary_modal_name)
related_modal_names = self.class.where(:id.ne=>self.id).pluck(:related_modal_name).flatten.uniq
other_modal_names = primary_modal_names + related_modal_names
all_modal_names = self.class.get_modal_names_cache
except_modals = Dir.glob("tmp/#{self.module_name}/app/models/*.rb").map{|f|
fn = File.basename(f)
fn.slice(0,fn.length - 3)
}
all_modal_names = all_modal_names - except_modals
all_modal_names = all_modal_names + other_modal_names
all_modal_names = all_modal_names.uniq
self_modal_names = ([self.primary_modal_name]+self.related_modal_name).flatten
if self_modal_names.select{|n| all_modal_names.include?(n)}.count != 0
return false
end
end
def self.get_modal_names
name_routes = Rails.application.routes.named_routes.map{|k,v| k}.select{|s| s.to_s[0..4] == "admin"}.map{|s| s.to_s.split('admin_').last}
modal_names = name_routes.map{|n| n.classify}.select{|n| (n.constantize rescue nil)}
modal_names = modal_names.map{|n| n.constantize}.select{|n| n.class == Class }.uniq
@@modal_names = modal_names.map{|n| n.to_s.underscore}
end
def self.get_modal_names_cache
@@modal_names ||= get_modal_names
end
def restart_server
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 2
end
end

View File

@ -239,7 +239,26 @@
$.post("<%=check_plugin_exist_admin_personal_plugin_field_path%>",{plugin_name: $("#personal_plugin_field_module_name").val(), id: "<%=f.object.id %>"}).done(function(data){
console.log(data);
if(data["can_install"]){
$("#personal_plugin_field_module_name").css('border', '');
can_install = true;
var modal_names = $("[name*=modal_name]").map(function(i,v){return v.value})
modal_names = Array.from(modal_names);
$.post("<%=check_modal_name_admin_personal_plugin_field_path%>",{modal_names: modal_names, id: "<%=f.object.id %>"}).done(function(data){
if(!data["success"]){
var invalid_modal_names = data["invalid_modal_names"];
console.log(invalid_modal_names)
$("[name*=modal_name]").each(function(i,v){
if(invalid_modal_names.indexOf($(v).val()) != -1){
$(v).css("border", '2px solid red');
window.location.href = "#" + $(".main-forms")[0].id;
}else{
$(v).css("border", '');
}
})
can_install = false;
}
})
}else{
$("#personal_plugin_field_module_name").css('border', '2px solid red');
alert("<%=thead_field("please_change_module_name")%>");

View File

@ -1,5 +1,6 @@
<%= form_for @personal_plugin_field, url: admin_personal_plugin_fields_path, html: {class: "form-horizontal main-forms previewable"} do |f| %>
<fieldset>
<%= f.hidden_field :copy_id, :value => params[:personal_plugin_field_id] %>
<%= render partial: 'form', locals: {f: f} %>
</fieldset>
<% end %>

View File

@ -25,6 +25,7 @@
<h4><%= thead_field('backend_page') %></h4>
<div id="backend_page">
<%= render :partial => 'render_fields_check_table',locals: {:f=>f,:root_name=>'backend_fields',:page_name=>'index'} %>
<%= render :partial => 'render_fields_check_table',locals: {:f=>f,:root_name=>'backend_fields',:page_name=>'profile'} %>
<%= render :partial => 'render_fields_check_table',locals: {:f=>f,:root_name=>'backend_fields',:page_name=>'analysis',:access_field_types=>["date","date_time","year","year_month","time"]} %>
</div>
<h4><%= thead_field('frontend_page') %></h4>

View File

@ -39,6 +39,7 @@
})
var url = $(this).data("url");
$.get(url).done(function(data){
console.log(data);
$("#preloader").css("display",'none');
$("#generating_plugin").css("display",'none');
if(data["success"]){

View File

@ -29,6 +29,7 @@ en:
fields_display_setting: Fields display setting
backend_page: Backend Page
frontend_page: Frontend Page
profile: Personal Profile
index: Index Page
show: Show Page
member_show: Member show page

View File

@ -29,6 +29,7 @@ zh_tw:
fields_display_setting: 欄位顯示設定
backend_page: 後台頁面
frontend_page: 前台頁面
profile: 個人資料
index: Index 頁面
show: Show 頁面
member_show: 會員show頁面

View File

@ -11,6 +11,7 @@ Rails.application.routes.draw do
end
resource :personal_plugin_field do
post 'check_plugin_exist' ,to: 'personal_plugin_fields#check_plugin_exist'
post 'check_modal_name' ,to: 'personal_plugin_fields#check_modal_name'
end
end
end