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

Add support for partially-shared source directories #145

Merged
merged 2 commits into from
Mar 21, 2022

Conversation

armanbilge
Copy link
Contributor

Fixes #17.

For a full-cross-project set up for platforms a, b, c, d, etc. (in this order), configures the standard source and resource directories under these directories:

  • a-b
  • a-c
  • a-d
  • b-c
  • b-d
  • c-d
  • a-b-c
  • a-b-d
  • a-c-d
  • b-c-d

This is in addition to the already supported shared, a, b, c, and d directories.

}

private def makePartiallySharedSettings(platforms: Seq[Platform])(mkSettings: Seq[Platform] => Seq[Setting[_]]): Map[Platform, Seq[Setting[_]]] = {
platforms.to[ListSet] // preserve the platform ordering
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 suppose another option here is to use SortedSet and impose a lexical ordering instead. This might be less surprising for users.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would definitely recommend using lexicographic ordering, indeed. CrossProject is not really designed with the idea that the platforms are ordered in any way, so relying on that order is brittle.

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, thanks.

@armanbilge armanbilge force-pushed the feature/m-of-n-dirs branch 4 times, most recently from 7a96e24 to 4f50dc9 Compare February 21, 2022 13:36
@armanbilge
Copy link
Contributor Author

@sjrd polite bump? :)

@armanbilge
Copy link
Contributor Author

@sjrd I know sbt-crossproject PRs are no fun but whenever you can take a look would be much appreciated. Thanks! :)

@sjrd
Copy link
Collaborator

sjrd commented Mar 14, 2022

Right. I keep forgetting it. I'm on vacation this week but I'll have a look next week.

@armanbilge
Copy link
Contributor Author

Have a nice vacation! :)

require(List(isJVM, isJS, isNative).count(identity) == 1)

if (isJVM)
require(JSOrJVM.check && JVMOrNative.check && !JSOrNative.check)
Copy link
Collaborator

Choose a reason for hiding this comment

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

assert would make more sense than require here. Same for the two below.

Comment on lines 5 to 7
import scala.collection.immutable.HashMap
import scala.collection.immutable.ListSet
import scala.language.implicitConversions
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
import scala.collection.immutable.HashMap
import scala.collection.immutable.ListSet
import scala.language.implicitConversions
import scala.language.implicitConversions
import scala.collection.immutable.HashMap
import scala.collection.immutable.ListSet

Language imports should always come first, in a dedicated "paragraph".

Comment on lines 107 to 106
if (platforms.isEmpty) {
None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Every implementation will always have to do that. Instead, we should make it a pre-condition of partiallyShared{Src,Resources}Dir that platforms.nonEmpty. This is a bit more work for the (unique) caller of these methods, and less work for everyone who would like to implement CrossType.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is still relevant, I believe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woops, I thought I got this. Must have been another one 🤔

Comment on lines 313 to 317
.map { platformSubset => // make the settings for this subset of platforms
val settings = mkSettings(platformSubset)
platformSubset.map(_ -> settings).toMap
}
.fold(platforms.map(_ -> Seq.empty).toMap)(mergeMapSeq)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems complicated and non intuitive to me. I think it may be reinventing a groupBy + flatMap. Can't it be rewritten as follows:

Suggested change
.map { platformSubset => // make the settings for this subset of platforms
val settings = mkSettings(platformSubset)
platformSubset.map(_ -> settings).toMap
}
.fold(platforms.map(_ -> Seq.empty).toMap)(mergeMapSeq)
.flatMap { platformSubset => // make the settings for this subset of platforms
val settings = mkSettings(platformSubset)
platformSubset.map(_ -> settings)
} // Seq[(Platform, Seq[Setting])]
.groupBy(_._1).map(kv => kv._1 -> kv._2.flatMap(_._2))

The last line would be easier with Scala 2.13's groupMapReduce, but we can't use it since this is 2.12 code. It would be written as

.groupMapReduce(_._1)(_._2)(_ ++ _)

Copy link
Collaborator

Choose a reason for hiding this comment

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

The last line can even probably be delayed until the very end of shared{Src,Resources}Dir. Until then, we would keep using Seq[(Platform, Seq[Setting])] everywhere. This would entirely avoid passing through merge/toHashMap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, this was a big big improvement 😅

)
)
}.toMap

new CrossProject(id, crossType, projects)
}

private def sharedSrcSettings(crossType: CrossType): Seq[Setting[_]] = {
private def sharedSrcSettings(
platforms: Seq[Platform],
Copy link
Collaborator

Choose a reason for hiding this comment

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

This parameter is redundant. It is already accessible as an instance field.

@armanbilge armanbilge force-pushed the feature/m-of-n-dirs branch 2 times, most recently from 84c3b81 to b61c500 Compare March 21, 2022 14:33
@armanbilge armanbilge requested a review from sjrd March 21, 2022 14:50
.map(_.toSeq.sortBy(_.identifier)) // use a consistent ordering
.flatMap { platformSubset => // make the settings for this subset of platforms
val settings = mkSettings(platformSubset)
platformSubset.map(_ -> settings).toMap
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
platformSubset.map(_ -> settings).toMap
platformSubset.map(_ -> settings)

Comment on lines 107 to 106
if (platforms.isEmpty) {
None
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is still relevant, I believe.

@armanbilge
Copy link
Contributor Author

@sjrd thanks for the review! Too much to ask for a 1.2.0? 😛

@sjrd sjrd merged commit 4ea98e3 into portable-scala:master Mar 21, 2022
i10416 added a commit to i10416/scodec-bits that referenced this pull request Aug 22, 2024
This commit adds the following changes

- Move shared sources to js-native directory to reduce redundant files
    - See portable-scala/sbt-crossproject#145
- Port hash functions in pure Scala for js and native platform with
  reference to https://github.com/square/okio (Apache V2
License)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mid-term: sbt-cross might give hints about handling 2-out-of-3 (m-out-of-N) issues.
2 participants