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

[#333] Use key-value map for extra files #369

Merged
merged 2 commits into from
Nov 5, 2019

Conversation

chshersh
Copy link
Contributor

@chshersh chshersh commented Nov 4, 2019

Resolves #333
Resolves #360

It actually works! I tested with the file from org repo. And the tree displays perfectly! Also, Summoner.Shell module turned out to be redundant...

@chshersh chshersh added feature config TOML configurations enhancement labels Nov 4, 2019
@chshersh chshersh requested a review from vrom911 as a code owner November 4, 2019 20:33
@chshersh chshersh self-assigned this Nov 4, 2019
Copy link
Contributor

@hint-man hint-man bot left a comment

Choose a reason for hiding this comment

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

There is no place for me here... I will choose the truth I like.

@chshersh chshersh force-pushed the chshersh/333-Use-key-value-map-for-extra branch from 790f95b to 6f8bf23 Compare November 4, 2019 21:10
Copy link
Member

@vrom911 vrom911 left a comment

Choose a reason for hiding this comment

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

This is great work, thanks @chshersh ❤️

@@ -7,7 +7,6 @@ packages:
extra-deps:
- base-noprelude-4.13.0.0
- brick-0.50
- relude-0.6.0.0
Copy link
Member

Choose a reason for hiding this comment

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

🎉 we are in the nightly!

@@ -6,6 +6,7 @@ packages:

extra-deps:
- optparse-applicative-0.15.0.0
- relude-0.6.0.0
Copy link
Member

Choose a reason for hiding this comment

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

I left 0.5.0 intentionally here, so we check with both versions, as our boundaries are including it, do you think that it is not important? Should we then update the boundaries in the cabal 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.

I've updated the boundaries in the .cabal file. I decided to bump up the lower bound because I'm using mapMaybeM function from a new version (it's very helpful 🎉) and summon simply won't compile with relude-0.5.0 😞 In general I don't mind supporting both versions but here I had to increase lower bound...

Copy link
Member

Choose a reason for hiding this comment

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

Oh, now I see!
You can put "Resolves #360 " then 🙂

{- | This function fetches contents of extra file sources.
-}
fetchSources :: Bool -> Map FilePath Source -> IO [TreeFs]
fetchSources isOffline = mapMaybeM sourceToTree . Map.toList
Copy link
Member

Choose a reason for hiding this comment

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

🎇 mapMaybeM 🎇

fetchSource isOffline source >>= \case
Nothing -> do
errorMessage $ "Error fetching: " <> toText path
pure Nothing
Copy link
Member

Choose a reason for hiding this comment

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

Is this correct, that in case of download (or just file getting) failure the process of the project generation will still continue without those files?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vrom911 I'm not sure, but implementing different logic is quite complicated. For example, if isOffile is True then all Url will fail. But maybe this is intentional and maybe I don't want things like CONTRIBUTING.md while developing locally 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Yes, makes sense. By the way, where the mentioned case is handled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's handled here:

Url url -> if isOffline
then Nothing <$ infoMessage ("Ignoring fetching from URL in offline mode from source: " <> url)
else fetchUrl url `catch` urlError url

Copy link
Member

Choose a reason for hiding this comment

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

Oh, right! Thank you 🍷

(File _ _, Dir _ _) -> x : insertTree node xs
(File nodePath _, File curPath _)
| nodePath == curPath -> node : xs
| otherwise -> x : insertTree node xs
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite get this case. When this file is already in the tree, it inserts it, but if it's not, it somehow continues inserting on the next layer? How so?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, the implementation is not obvious, but I tried to do my best. This function always inserts node to the list, at least when it reaches the empty list at the end:

insertTree node [] = [node]

In this case, if this function finds two Files with the same path, it will replace current x with node that why it's node : xs. This is done if people want to override default files like .travis.yml or Prelude.hs with their files. If file paths are not equal, then we keep x, and we will try to insert node into xs. Eventually, we either reach the end of the list, and we will add node unconditionally, or we will find a File that has the same name and will replace it.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, but are other nodes placed unconditionally? I thought there is some kind of hierarchy so no need to go till the end of the list each time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, this list is not sorted at this stage. It could be [File "foo" x, File "bar" y] or [File "bar" x, File "foo" y]. I checked the implementation, and it looks like we sort this list only when printing to terminal or in TUI.

Now, after you told this, I started to think that probably there's a better way to implement TreeFs. At least TreeFs shouldn't have duplicate filepaths but since we're using lists, it's possible to have a bug in the implementation and introduce duplicate fields.

Maybe instead of

data TreeFs
    = Dir FilePath [TreeFs]
    | File FilePath Text

we should have

data TreeFs
    = Dir (Map FilePath TreeFs)
    | File Text

The latter representation won't allow having duplicate filepaths. But I believe that the current algorithm is also correct, even if the logic is not straightforward... And changing this type is a big rewrite...

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for the clarification, Dmitrii! Yeah, I don't think that rewrite is the right step right now, and we could keep this function as it is for this PR. But let's think more about this sorting before option and create the issue if we will decide that it could work better and would become easier to maintain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

@vrom911 vrom911 left a comment

Choose a reason for hiding this comment

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

Great! Thanks a lot 👏
Now, let the testing period begin!

@vrom911 vrom911 merged commit c10b447 into map Nov 5, 2019
@vrom911 vrom911 deleted the chshersh/333-Use-key-value-map-for-extra branch November 5, 2019 07:30
chshersh added a commit that referenced this pull request Nov 5, 2019
* [#333] Use key-value map for extra files

Resolves #333

* Fix stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants