Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java Lab: add helper to get all project files #44871

Merged
merged 5 commits into from
Feb 18, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions dashboard/app/helpers/javalab_files_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

module JavalabFilesHelper
# Get all files related to the project at the given channel id as a hash. The hash is in the format
# below. All values are StringIO.
# {
# "main.json": <main source file for a project>
# "assets": {"asset_name_1": <asset_value>, ...}
# "validation": <all validation code for a project, in json format>
# "maze": <serialized maze if it exists>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I was originally thinking that we'd store both main.json and the maze file in the same sources/ folder. My thinking was that if we eventually have other level content, then it could all just reside in sources. That being said though, the maze file isn't really a source file so I can also see a case for it being in its own directory. I'm fine with this being separate, we'll just have to fetch it accordingly in Javabuilder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong preference here. Would it be better to do the below? That way we have 3 clear categories instead of main.json being an outlier:

{
  "sources": {"main.json": "...", "grid.txt": "..."},
  "assets": {...},
  "validation": {...}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah, I think having three categories is clearer and more extensible if we want to add more to sources down the line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 will update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

# }
# If the channel doesn't have validation and/or a maze, those fields will not be present.
def self.get_project_files(channel_id)
storage_id, storage_app_id = storage_decrypt_channel_id(channel_id)
all_files = {}
# get sources file
source_data = SourceBucket.new.get(channel_id, "main.json")
# Note: we can call .string on this value (and all other values for files) to get the raw string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - for reference, I'm looking into zipping files in rails right now, and passing a string value for each file seems to be the easiest option, so this will work well!

all_files["main.json"] = source_data[:body]
# get starter assets
assets = {}
asset_bucket = AssetBucket.new
channel = ChannelToken.where(storage_app_id: storage_app_id, storage_id: storage_id).first
level = Level.cache_find(channel.level_id)
if level
starter_asset_bucket = Aws::S3::Bucket.new(LevelStarterAssetsController::S3_BUCKET)
(level&.project_template_level&.starter_assets || level.starter_assets || []).map do |friendly_name, uuid_name|
filename = "#{LevelStarterAssetsController::S3_PREFIX}#{uuid_name}"
asset = starter_asset_bucket.object(filename)
assets[friendly_name] = asset.get.body
end
end
# get level assets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may want to switch the order here and fetch starter assets after user assets - we currently have logic (both in Javabuilder and in the asset manager I believe) that if a starter asset file and a user asset file have the same name, we use the starter asset file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call out, will reverse this

asset_list = asset_bucket.list(channel_id)
asset_list.each do |asset|
assets[asset[:filename]] = asset_bucket.get(channel_id, asset[:filename])[:body]
end
all_files["assets"] = assets
# get validation code
if level.respond_to?(:validation) && level.validation
all_files["validation"] = StringIO.new(level.validation.to_json)
end
# get maze file
serialized_maze = level.try(:get_serialized_maze)
if serialized_maze
all_files["maze"] = StringIO.new(serialized_maze.to_s)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to call to_json first on the maze file? That's what I was doing in my prototype, but not sure if it's necessary for javabuilder to parse it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like both to_json and to_s do the same thing. The maze is an array so it seems it works either way.

end
return all_files
end
end