Skip to content

Commit

Permalink
Solara
Browse files Browse the repository at this point in the history
  • Loading branch information
MalekKamel committed Sep 25, 2024
1 parent 89c846a commit c6282e4
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 106 deletions.
238 changes: 163 additions & 75 deletions solara/lib/core/aliases/alias_generator.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,72 @@
Dir[File.expand_path('../scripts/*.rb', __dir__)].each { |file| require_relative file }
require 'json'

class AliasGenerator
class AliasManager
def initialize
@output_file_unix = FilePath.solara_generated_aliases_unix
@output_file_windows = FilePath.solara_generated_aliases_windows
FileManager.create_file_if_not_exist(@output_file_unix)
FileManager.create_file_if_not_exist(@output_file_windows)
if windows?
@generator = WindowsAliasGenerator.new
else
@generator = UnixAliasGenerator.new
end
end

@readme_file = FilePath.solara_aliases_readme
FileManager.create_file_if_not_exist(@readme_file)
def start
Solara.logger.start_step("Generate terminal command aliases")
add_brand_aliases(BrandsManager.instance.brands_list)

@json_file = FilePath.solara_aliases_json
FileManager.create_file_if_not_exist(@json_file)
common_aliases = {
"solara_dashboard" => "bundle exec solara dashboard",
"solara_doctor" => "bundle exec solara doctor",
"solara_status" => "bundle exec solara status"
}
add_common_aliases(common_aliases)

generate_shell_file
save_aliases_to_json
generate_readme

SolaraSetup.new.run

Solara.logger.end_step("Generate terminal command aliases")
end

private

def add_brand_aliases(brands)
@generator.add_brand_aliases(brands)
end

def add_common_aliases(common_aliases)
@generator.add_common_aliases(common_aliases)
end

def generate_shell_file
@generator.generate_shell_file
end

def generate_readme
@generator.generate_readme
end

def save_aliases_to_json
@generator.save_aliases_to_json
end

def self.aliases_json
path = FilePath.solara_aliases_json
JSON.parse(File.read(path))
end

private

def windows?
# Check for Windows OS
!!(RUBY_PLATFORM =~ /mingw|mswin/)
end
end

class AliasGenerator
def initialize
@brand_aliases = {}
@common_aliases = []
end
Expand Down Expand Up @@ -42,17 +95,81 @@ def add_common_aliases(common_aliases)
@common_aliases = common_aliases
end

def generate_shell_files
generate_unix_shell_file
generate_windows_shell_file
def generate_readme
File.open(@readme_file, 'w') do |file|
file.puts "# Aliases"
file.puts
file.puts "This document provides an overview of all available aliases."
file.puts

if @common_aliases.any?
file.puts "## Common Aliases"
file.puts
@common_aliases.each do |alias_name, command|
file.puts "- `#{alias_name}`: `#{command}`"
end
end

file.puts

@brand_aliases.each do |brand_name, brand_aliases|
file.puts "## #{brand_name}"
file.puts
brand_aliases.each do |alias_name, command|
file.puts "- `#{alias_name}`: `#{command}`"
end
file.puts
end
end
Solara.logger.debug("README.md has been generated in #{@readme_file}")
end

def save_aliases_to_json
json_data = {
"common_aliases" => @common_aliases,
"brand_aliases" => @brand_aliases
}

File.open(@json_file, 'w') do |file|
file.puts JSON.pretty_generate(json_data)
end
Solara.logger.debug("Aliases have been saved in JSON format in #{@json_file}")
end
end

def generate_unix_shell_file
existing_content = File.exist?(@output_file_unix) ? File.read(@output_file_unix) : ""
class UnixAliasGenerator < AliasGenerator
def initialize
super()
@output_file = FilePath.solara_generated_aliases_unix
@readme_file = FilePath.solara_aliases_readme
@json_file = FilePath.solara_aliases_json
FileManager.create_file_if_not_exist(@output_file)
FileManager.create_file_if_not_exist(@readme_file)
FileManager.create_file_if_not_exist(@json_file)
end

def generate_shell_file
existing_content = File.exist?(@output_file) ? File.read(@output_file) : ""
new_content = []

new_content << "#!/bin/bash" if existing_content.empty?

add_common_aliases_to_file(existing_content, new_content)
add_brand_aliases_to_file(existing_content, new_content)

if new_content.any? && !(new_content.size == 1 && new_content.first == "#!/bin/bash")
File.open(@output_file, 'a') do |file|
file.puts new_content
end
Solara.logger.debug("Unix aliases have been appended to #{@output_file}")
else
Solara.logger.debug("No new Unix aliases to add to #{@output_file}")
end
end

private

def add_common_aliases_to_file(existing_content, new_content)
common_aliases_added = false
@common_aliases.each do |alias_name, command|
alias_line = "alias #{alias_name}='#{command}'"
Expand All @@ -62,7 +179,9 @@ def generate_unix_shell_file
new_content << alias_line
end
end
end

def add_brand_aliases_to_file(existing_content, new_content)
brand_aliases_added = false
@brand_aliases.each do |brand_name, brand_aliases|
brand_aliases_for_this_brand_added = false
Expand All @@ -81,25 +200,43 @@ def generate_unix_shell_file
end
end
end

new_content.reject!(&:empty?) if new_content.size == 1
end
end

if new_content.any? && !(new_content.size == 1 && new_content.first == "#!/bin/bash")
File.open(@output_file_unix, 'a') do |file|
class WindowsAliasGenerator < AliasGenerator
def initialize
super()
@output_file = FilePath.solara_generated_aliases_windows
@readme_file = FilePath.solara_aliases_readme
@json_file = FilePath.solara_aliases_json
FileManager.create_file_if_not_exist(@output_file)
FileManager.create_file_if_not_exist(@readme_file)
FileManager.create_file_if_not_exist(@json_file)
end

def generate_shell_file
existing_content = File.exist?(@output_file) ? File.read(@output_file) : ""
new_content = []

new_content << "@echo off" if existing_content.empty?

add_common_aliases_to_file(existing_content, new_content)
add_brand_aliases_to_file(existing_content, new_content)

if new_content.any? && !(new_content.size == 1 && new_content.first == "@echo off")
File.open(@output_file, 'a') do |file|
file.puts new_content
end
Solara.logger.debug("Unix aliases have been appended to #{@output_file_unix}")
Solara.logger.debug("Windows aliases have been appended to #{@output_file}")
else
Solara.logger.debug("No new Unix aliases to add to #{@output_file_unix}")
Solara.logger.debug("No new Windows aliases to add to #{@output_file}")
end
end

def generate_windows_shell_file
existing_content = File.exist?(@output_file_windows) ? File.read(@output_file_windows) : ""
new_content = []

new_content << "@echo off" if existing_content.empty?
private

def add_common_aliases_to_file(existing_content, new_content)
common_aliases_added = false
@common_aliases.each do |alias_name, command|
alias_line = "doskey #{alias_name}=#{command} $*"
Expand All @@ -109,7 +246,9 @@ def generate_windows_shell_file
new_content << alias_line
end
end
end

def add_brand_aliases_to_file(existing_content, new_content)
brand_aliases_added = false
@brand_aliases.each do |brand_name, brand_aliases|
brand_aliases_for_this_brand_added = false
Expand All @@ -128,57 +267,6 @@ def generate_windows_shell_file
end
end
end

new_content.reject!(&:empty?) if new_content.size == 1

if new_content.any? && !(new_content.size == 1 && new_content.first == "@echo off")
File.open(@output_file_windows, 'a') do |file|
file.puts new_content
end
Solara.logger.debug("Windows aliases have been appended to #{@output_file_windows}")
else
Solara.logger.debug("No new Windows aliases to add to #{@output_file_windows}")
end
end

def generate_readme
File.open(@readme_file, 'w') do |file|
file.puts "# Aliases"
file.puts
file.puts "This document provides an overview of all available aliases."
file.puts

if @common_aliases.any?
file.puts "## Common Aliases"
file.puts
@common_aliases.each do |alias_name, command|
file.puts "- `#{alias_name}`: `#{command}`"
end
end

file.puts

@brand_aliases.each do |brand_name, brand_aliases|
file.puts "## #{brand_name}"
file.puts
brand_aliases.each do |alias_name, command|
file.puts "- `#{alias_name}`: `#{command}`"
end
file.puts
end
end
Solara.logger.debug("README.md has been generated in #{@readme_file}")
end

def save_aliases_to_json
json_data = {
"common_aliases" => @common_aliases,
"brand_aliases" => @brand_aliases
}

File.open(@json_file, 'w') do |file|
file.puts JSON.pretty_generate(json_data)
end
Solara.logger.debug("Aliases have been saved in JSON format in #{@json_file}")
end
end
29 changes: 0 additions & 29 deletions solara/lib/core/aliases/alias_generator_manager.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def mount
end

def get_brand_aliases
AliasGeneratorManager.aliases_json
AliasManager.aliases_json
rescue StandardError => e
Solara.logger.failure("Error getting brand aliases: #{e.message}")
raise
Expand Down
2 changes: 1 addition & 1 deletion solara/lib/core/solara_configurator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize

def start
GitignoreManager.ignore
AliasGeneratorManager.new.start
AliasManager.new.start
end

end
Expand Down

0 comments on commit c6282e4

Please sign in to comment.