Skip to content

Commit

Permalink
Add template sync and JSON file viewer in dashboard
Browse files Browse the repository at this point in the history
- Display user-defined JSON files in dashboard
- Add mechanism to sync existing brand with template changes
  • Loading branch information
MalekKamel committed Oct 28, 2024
1 parent 5784e1b commit 1b778b6
Show file tree
Hide file tree
Showing 26 changed files with 371 additions and 143 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
solara (0.4.0)
solara (0.6.0)
cgi (~> 0.4.1)
colorize (~> 1.1.0)
json-schema (~> 4.3.1)
Expand Down
54 changes: 29 additions & 25 deletions solara/lib/core/brands/brand_onboarder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,47 @@
Dir[File.expand_path('platform/flutter/*.rb', __dir__)].each { |file| require_relative file }

class BrandOnboarder
def initialize(brand_key, brand_name, clone_brand_key: nil)
@brand_key = brand_key
@brand_name = brand_name
@clone_brand_key = clone_brand_key
end

def onboard
if @clone_brand_key.nil? || @clone_brand_key.strip.empty?
generate_brand_template
def onboard(brand_key, brand_name, clone_brand_key: nil)
if clone_brand_key.nil? || clone_brand_key.strip.empty?
generate_from_template(brand_key)
else
clone_brand
clone_brand(brand_key, clone_brand_key: clone_brand_key)
end
add_to_brands_list
add_to_brands_list(brand_key, brand_name)
end

def generate_brand_template
Solara.logger.debug("Onboarding #{@brand_key} from template.")

template_dir = FilePath.template_brands
target_dir = FilePath.brand(@brand_key)
config_file = FilePath.template_config

generator = ProjectTemplateGenerator.new(template_dir, target_dir, config_file)
generator.create_project
def sync_with_template(brand_key)
generator = template_generator(brand_key)
generator.sync_with_template
end

def clone_brand
Solara.logger.debug("Cloning #{@clone_brand_key} to #{@brand_key}")
source = FilePath.brand(@clone_brand_key)
destination = FilePath.brand(@brand_key)
def clone_brand(brand_key, clone_brand_key:)
Solara.logger.debug("Cloning #{clone_brand_key} to #{brand_key}")
source = FilePath.brand(clone_brand_key)
destination = FilePath.brand(brand_key)

FileManager.delete_if_exists(destination)
FolderCopier.new(source, destination).copy
end

def add_to_brands_list
BrandsManager.instance.add_brand(@brand_name, @brand_key)
def add_to_brands_list(brand_key, brand_name)
BrandsManager.instance.add_brand(brand_name, brand_key)
end

def generate_from_template(brand_key)
Solara.logger.debug("Onboarding #{brand_key} from template.")

generator = template_generator(brand_key)
generator.create_project
end

def template_generator(brand_key)
template_dir = FilePath.template_brands
target_dir = FilePath.brand(brand_key)
config_file = FilePath.template_config

ProjectTemplateGenerator.new(template_dir, target_dir, config_file)
end

end
Expand Down
1 change: 1 addition & 0 deletions solara/lib/core/brands/brand_switcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def initialize(brand_key, ignore_health_check: false)
def start
Solara.logger.header("Switching to #{@brand_key}")

BrandOnboarder.new.sync_with_template(@brand_key)
@health_checker.check_health
BrandsManager.instance.save_current_brand(@brand_key)
@artifacts_switcher.switch
Expand Down
2 changes: 1 addition & 1 deletion solara/lib/core/dashboard/brand/BrandDetailController.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class BrandDetailController {
if (this.model.isRemote()) return

try {
await this.model.saveSection(container.dataset.key, section.content);
await this.model.saveSection(container.dataset.filename, section.content);
await this.checkBrandHealth();
if (this.model.isCurrentBrand) {
await this.switchToBrand(true)
Expand Down
6 changes: 3 additions & 3 deletions solara/lib/core/dashboard/brand/SectionsFormManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SectionsFormManager {
const sectionElement = document.createElement('div');
sectionElement.className = 'section';

sectionElement.dataset.key = section.key
sectionElement.dataset.filename = section.filename
sectionElement.dataset.name = section.name

const titleContainer = document.createElement('div');
Expand All @@ -35,7 +35,7 @@ class SectionsFormManager {

sectionElement.appendChild(titleContainer);

sectionElement.id = section.key;
sectionElement.id = section.filename;

this.sectionsContainer.appendChild(sectionElement);

Expand All @@ -61,7 +61,7 @@ class SectionItemManager {
displayJSONCards() {
let cardContent = this.container.querySelector('.card-content')
if (cardContent !== null) this.container.innerHTML = ''
this.container.appendChild(this.createCard(this.section.content, 'root', null, this.section.key));
this.container.appendChild(this.createCard(this.section.content, 'root', null, this.section.filename));
}

createCard(obj, key, parent, cardTitle) {
Expand Down
4 changes: 2 additions & 2 deletions solara/lib/core/dashboard/brand/brand.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
.right {
width: 15%;
position: fixed;
max-height: 50%;
max-height: 95%;
overflow-y: auto;
padding: 7px;
right: 0;
Expand Down Expand Up @@ -253,7 +253,7 @@
#error-button {
position: fixed;
bottom: 14px;
right: 14px;
left: 14px;
background-color: #ff4136;
color: white;
border: none;
Expand Down
4 changes: 2 additions & 2 deletions solara/lib/core/dashboard/brand/source/BrandLocalSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class BrandLocalSource {
}
}

async saveSection(key, configuration, brandKey) {
async saveSection(filename, configuration, brandKey) {
if (this.savingInProgress) return;
this.savingInProgress = true;

const dataToSend = {
brand_key: brandKey,
key: key,
filename: filename,
data: configuration
};

Expand Down
20 changes: 18 additions & 2 deletions solara/lib/core/dashboard/brand/source/BrandRemoteSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class BrandRemoteSource {
}
const content = await contentResponse.json();
return {
key: config.key,
name: config.name,
filename: config.filename,
name: this.snakeToCapitalizedSpaced(config.filename, 'ios'),
content: content
};
});
Expand All @@ -36,6 +36,22 @@ class BrandRemoteSource {
}
}

// TODO: should be ecnapsulated
snakeToCapitalizedSpaced(
snakeCaseString,
exclude = '',
transform = (item) => item.charAt(0).toUpperCase() + item.slice(1)
) {
// Split by underscores
const parts = snakeCaseString.split('_').map(item => {
// Return the item as-is if it matches the exclude value
return item === exclude ? item : transform(item);
});

// Join the parts with a space
return parts.join(' ');
}

async getBrandConfigurationsJsonFromDirectory(dirHandle) {
const schema = await this.fetchBrandConfigurationsSchema();

Expand Down
22 changes: 5 additions & 17 deletions solara/lib/core/dashboard/handler/edit_section_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ def mount
begin
request_payload = JSON.parse(req.body)
brand_key = request_payload['brand_key']
key = request_payload['key']
filename = request_payload['filename']
data = request_payload['data']

update_section(key, data, brand_key)
update_section(filename, data, brand_key)
set_current_brand_content_changed(brand_key, true)

res.status = 200
res.body = JSON.generate({ success: true, message: "Configuration for #{key} updated successfully" })
res.body = JSON.generate({ success: true, message: "Configuration for #{filename} updated successfully" })
res.content_type = 'application/json'
rescue JSON::ParserError => e
handle_error(res, e, "Invalid JSON in request body", 400)
Expand All @@ -27,20 +27,8 @@ def mount
end
end

def update_section(key, data, brand_key)
template = BrandConfigurationsManager.new(brand_key).template_with_key(key)

path = template[:path]

if File.exist?(path)
File.write(path, JSON.pretty_generate(data))
Solara.logger.debug("Updated Config for #{path}: #{data}")
else
raise "Config file not found: #{path}"
end
rescue StandardError => e
Solara.logger.failure("Error updating section: #{e.message}")
raise
def update_section(filename, data, brand_key)
BrandConfigUpdater.new.update(filename, data, brand_key)
end

def set_current_brand_content_changed(brand_key, changed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ structure:
json:
type: directory
contents:
json_manifest.json:
android_json_manifest.json:
type: file
validations:
- type: valid_json
Expand Down Expand Up @@ -63,7 +63,7 @@ structure:
json:
type: directory
contents:
json_manifest.json:
ios_json_manifest.json:
type: file
validations:
- type: valid_json
Expand Down Expand Up @@ -94,7 +94,7 @@ structure:
json:
type: directory
contents:
json_manifest.json:
global_json_manifest.json:
type: file
validations:
- type: valid_json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ structure:
json:
type: directory
contents:
json_manifest.json:
flutter_json_manifest.json:
type: file
validations:
- type: valid_json
Expand Down Expand Up @@ -89,7 +89,7 @@ structure:
json:
type: directory
contents:
json_manifest.json:
global_json_manifest.json:
type: file
validations:
- type: valid_json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ structure:
json:
type: directory
contents:
json_manifest.json:
android_json_manifest.json:
type: file
validations:
- type: valid_json
Expand Down Expand Up @@ -63,7 +63,7 @@ structure:
json:
type: directory
contents:
json_manifest.json:
ios_json_manifest.json:
type: file
validations:
- type: valid_json
Expand Down Expand Up @@ -94,7 +94,7 @@ structure:
json:
type: directory
contents:
json_manifest.json:
global_json_manifest.json:
type: file
validations:
- type: valid_json
Expand Down
25 changes: 25 additions & 0 deletions solara/lib/core/scripts/brand_config_updater.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class BrandConfigUpdater

def update(filename, data, brand_key)
template = BrandConfigurationsManager.new(brand_key).template_with_filename(filename)

path = template[:path]
json = JSON.pretty_generate(data)

begin
# Check if the file exists
if File.exist?(path)
# If it exists, update the file
File.write(path, json)
Solara.logger.debug("Updated Config for #{path}: #{data}")
else
# If it doesn't exist, create the file with the data
File.write(path, json)
Solara.logger.debug("Created Config for #{path}: #{data}")
end
rescue StandardError => e
Solara.logger.failure("Error updating #{brand_key} config file #{path}: #{e.message}")
raise
end
end
end
Loading

0 comments on commit 1b778b6

Please sign in to comment.