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 3d02365 commit bf2ada7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 30 deletions.
64 changes: 44 additions & 20 deletions solara/lib/core/aliases/alias_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def start
]
add_common_aliases(common_aliases)

generate_shell_file
generate
save_aliases_to_json
generate_readme

Expand All @@ -46,8 +46,8 @@ def add_common_aliases(common_aliases)
@generator.add_common_aliases(common_aliases)
end

def generate_shell_file
@generator.generate_shell_file
def generate
@generator.generate
end

def generate_readme
Expand Down Expand Up @@ -155,7 +155,7 @@ def initialize
FileManager.create_file_if_not_exist(@json_file)
end

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

Expand Down Expand Up @@ -214,60 +214,84 @@ def add_brand_aliases_to_file(existing_content, new_content)
class WindowsAliasGenerator < AliasGenerator
def initialize
super()
@output_file = FilePath.solara_generated_aliases_windows
@cmd_output_file = FilePath.solara_generated_aliases_windows_command_prompt
@ps_output_file = FilePath.solara_generated_aliases_powershell
@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(@cmd_output_file)
FileManager.create_file_if_not_exist(@ps_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) : ""
def generate
generate_cmd_file
generate_powershell_file
end

private

def generate_cmd_file
existing_content = File.exist?(@cmd_output_file) ? File.read(@cmd_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)
add_common_aliases_to_file(existing_content, new_content, :cmd)
add_brand_aliases_to_file(existing_content, new_content, :cmd)

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

private
def generate_powershell_file
existing_content = File.exist?(@ps_output_file) ? File.read(@ps_output_file) : ""
new_content = []

def add_common_aliases_to_file(existing_content, new_content)
add_common_aliases_to_file(existing_content, new_content, :powershell)
add_brand_aliases_to_file(existing_content, new_content, :powershell)

if new_content.any?
File.open(@ps_output_file, 'a') do |file|
file.puts new_content
end
Solara.logger.debug("PowerShell aliases have been appended to #{@ps_output_file}")
else
Solara.logger.debug("No new PowerShell aliases to add to #{@ps_output_file}")
end
end

def add_common_aliases_to_file(existing_content, new_content, shell_type)
common_aliases_added = false
@common_aliases.each do |alias_name, command|
alias_line = "doskey #{alias_name}=#{command} $*"
alias_line = shell_type == :cmd ? "doskey #{alias_name}=#{command} $*" : "function #{alias_name} { & #{command} }"
unless existing_content.include?(alias_line)
new_content << "REM Common Aliases" unless common_aliases_added
new_content << (shell_type == :cmd ? "REM Common Aliases" : "# Common Aliases") unless common_aliases_added
common_aliases_added = true
new_content << alias_line
end
end
end

def add_brand_aliases_to_file(existing_content, new_content)
def add_brand_aliases_to_file(existing_content, new_content, shell_type)
brand_aliases_added = false
@brand_aliases.each do |brand_name, brand_aliases|
brand_aliases_for_this_brand_added = false
brand_aliases.each do |alias_name, command|
alias_line = "doskey #{alias_name}=#{command} $*"
alias_line = shell_type == :cmd ? "doskey #{alias_name}=#{command} $*" : "function #{alias_name} { & #{command} }"
unless existing_content.include?(alias_line)
unless brand_aliases_added
brand_aliases_added = true
end
unless brand_aliases_for_this_brand_added
new_content << "" # Add a new line before each brand name
new_content << "REM #{brand_name}"
new_content << (shell_type == :cmd ? "REM #{brand_name}" : "# #{brand_name}")
brand_aliases_for_this_brand_added = true
end
new_content << alias_line
Expand Down
38 changes: 30 additions & 8 deletions solara/lib/core/aliases/terminal_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,51 @@ def determine_shell_config_file

class WindowsTerminalSetup
def initialize(setup_dir)
@windows_setup_file = FilePath.solara_generated_aliases_windows
puts "Setting AutoRun to: #{@windows_setup_file}" # Debug statement
end

def run
setup_windows
setup_command_prompt
setup_powershell
end

private

def setup_windows
def setup_command_prompt
require 'win32/registry'
setup_file = FilePath.solara_generated_aliases_windows_command_prompt

begin
Win32::Registry::HKEY_CURRENT_USER.create('Software\Microsoft\Command Processor') do |reg|
reg['AutoRun', Win32::Registry::REG_EXPAND_SZ] = @windows_setup_file
reg['AutoRun', Win32::Registry::REG_EXPAND_SZ] = setup_file
end
Solara.logger.success("Windows AutoRun registry key set up successfully.")
Solara.logger.warn("Please restart your Command Prompt to apply changes.")
Solara.logger.debug("Windows AutoRun registry key set up successfully.")
Solara.logger.debug("Please restart your Command Prompt to apply changes.")
rescue Win32::Registry::Error => e
Solara.logger.failure("Failed to set up Windows registry: #{e.class} - #{e.message}")
Solara.logger.failure("Please add the following file path to your AutoRun registry key manually:")
Solara.logger.failure(@windows_setup_file)
Solara.logger.failure(setup_file)
end
end

def setup_powershell_
require 'win32/registry'
setup_file = FilePath.solara_generated_aliases_powershell

begin
# This path targets all versions of PowerShell
Win32::Registry::HKEY_CURRENT_USER.create('Software\Microsoft\PowerShell\PowerShellEngine') do |reg|
reg['AutoRun', Win32::Registry::REG_EXPAND_SZ] = setup_file
end
Solara.logger.debug("PowerShell AutoRun registry key set up successfully.")
Solara.logger.debug("Please restart your PowerShell to apply changes.")
rescue Win32::Registry::Error => e
Solara.logger.failure("Failed to set up PowerShell registry: #{e.class} - #{e.message}")
Solara.logger.failure("Please add the following file path to your PowerShell AutoRun registry key manually:")
Solara.logger.failure(setup_file)
end
end

def setup_powershell
# Not supported at the moment
end
end
8 changes: 6 additions & 2 deletions solara/lib/core/scripts/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,12 @@ def self.solara_generated_aliases_unix
File.join(ENV['HOME'], '.solara', 'aliases.sh')
end

def self.solara_generated_aliases_windows
File.join(ENV['HOME'], '.solara', 'aliases.bat')
def self.solara_generated_aliases_windows_command_prompt
File.join(ENV['HOME'], '.solara', 'command_prompt_aliases.bat')
end

def self.solara_generated_aliases_powershell
File.join(ENV['HOME'], '.solara', 'powershell_aliases.ps1')
end

def self.solara_aliases_readme
Expand Down

0 comments on commit bf2ada7

Please sign in to comment.