-
Notifications
You must be signed in to change notification settings - Fork 5
Importing projects site projects from .sb3 files #855
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
Open
rammodhvadia
wants to merge
32
commits into
main
Choose a base branch
from
sb3parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
2dadc30
add starter yml
rammodhvadia dfe4b87
allow processing sb3 files
rammodhvadia 2c31782
initial parser attempt
rammodhvadia 2277ac0
update parser to work with GH webhook
rammodhvadia e539c07
implement scratch importing in project importer
rammodhvadia f058478
update upload job to accept and process sb3 files
rammodhvadia 29d0a7a
update filesystem_project for sb3 support
rammodhvadia fdc9728
tiny clean up on sb3 parser
rammodhvadia eb9cba1
Merge branch 'main' into sb3parser
rammodhvadia bddaab6
add asset importing for sb3 files
rammodhvadia 42c2ef2
update sb3 parser to pull files from zip
rammodhvadia e63e015
call asset importer in project importer
rammodhvadia 5a4c68b
fix import bug
rammodhvadia 676f9fe
refactor asset importer for consistency with original importer
rammodhvadia 888151c
clean up
rammodhvadia d9eed53
separate scratch component in upload job
rammodhvadia a6ac0ac
rubocop
rammodhvadia 5c0c65a
add sb3 parser tests
rammodhvadia 5ec7c72
clear up confusing naming
rammodhvadia 6ae3fd7
update tests for sb3 import functionality
rammodhvadia 490d99b
Merge branch 'main' into sb3parser
jamdelion 806e5e1
separate sb3 asset importer to it's own file
rammodhvadia e2fe5e3
default to importing first component item for scratch projects
rammodhvadia d13114c
scratch project config yaml doesn't need components section
rammodhvadia 9b53375
handle empty parsed_content for sb3 file
rammodhvadia 70dfe53
fix tests + rubocop
rammodhvadia 03d188d
Merge branch 'main' into sb3parser
rammodhvadia 9141993
fix tests
rammodhvadia ea51c47
add sb3 file for sample project
rammodhvadia 4bfe526
fix return conditions on project_importer
rammodhvadia 13b859f
add require zip for sb3 archive helper
rammodhvadia 8bb3e85
skip sb3 component import for non scratch projects
rammodhvadia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
| require 'marcel' | ||
| require 'stringio' | ||
| require 'zip' | ||
|
|
||
| class Sb3Parser | ||
| class MissingProjectJsonError < StandardError; end | ||
| class MissingAssetError < StandardError; end | ||
|
|
||
| attr_reader :component, :file_path, :io | ||
|
|
||
| def initialize(component: nil, file_path: nil) | ||
| @component = component | ||
| @file_path = component&.fetch(:file_path, nil) || file_path | ||
| @io = component&.fetch(:io, nil) | ||
| end | ||
|
|
||
| def parse | ||
| open_zip do |zip_file| | ||
| project_json = project_json_entry(zip_file) | ||
| content = JSON.parse(project_json.get_input_stream.read) | ||
|
|
||
| output = { | ||
| scratch_component: { content: }, | ||
| assets: assets(zip_file, extract_asset_names(content)) | ||
| } | ||
| output | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def open_zip(&) | ||
| return Zip::File.open(file_path, &) if file_path | ||
|
|
||
| io.rewind if io.respond_to?(:rewind) | ||
| result = nil | ||
| Zip::File.open_buffer(io.read) { |zip_file| result = yield zip_file } | ||
| result | ||
| end | ||
|
|
||
| def project_json_entry(zip_file) | ||
| zip_file.find_entry('project.json') || raise(MissingProjectJsonError, 'project.json not found in SB3 archive') | ||
| end | ||
|
|
||
| def extract_asset_names(value) | ||
| case value | ||
| when Hash | ||
| names = [] | ||
| names << value['md5ext'] if value['md5ext'].is_a?(String) | ||
| value.each_value { |item| names.concat(extract_asset_names(item)) } | ||
| names.uniq | ||
| when Array | ||
| value.flat_map { |item| extract_asset_names(item) }.uniq | ||
| else | ||
| [] | ||
| end | ||
| end | ||
|
|
||
| def assets(zip_file, asset_names) | ||
| asset_names.map do |asset_name| | ||
| entry = zip_file.find_entry(asset_name) || raise(MissingAssetError, "asset #{asset_name} not found in SB3 archive") | ||
| asset(entry) | ||
| end | ||
| end | ||
|
|
||
| def asset(entry) | ||
| io = StringIO.new(entry.get_input_stream.read) | ||
| content_type = Marcel::MimeType.for(io, name: entry.name) | ||
| io.rewind | ||
|
|
||
| { filename: entry.name, io:, content_type: } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'stringio' | ||
|
|
||
| class ScratchSb3AssetImporter | ||
| class << self | ||
| def import_all(assets) | ||
| assets.each do |asset| | ||
| new.import(asset) | ||
| rescue StandardError | ||
| next | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def import(asset) | ||
| create_asset(asset.fetch(:filename), asset.fetch(:io).read) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_asset(asset_name, content) | ||
| return if ScratchAsset.global_assets.exists?(filename: asset_name) | ||
|
|
||
| ScratchAsset.create!(filename: asset_name, project_id: nil, uploaded_user_id: nil) | ||
| .file | ||
| .attach(io: StringIO.new(content), filename: asset_name) | ||
| end | ||
|
rammodhvadia marked this conversation as resolved.
|
||
| end | ||
Binary file added
BIN
+2.36 KB
lib/tasks/project_components/scratch-integration-test-starter/main.sb3
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
lib/tasks/project_components/scratch-integration-test-starter/project_config.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| NAME: "scratch integration test" | ||
| IDENTIFIER: "editor-scratch-testing-starter" | ||
| TYPE: "code_editor_scratch" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.